Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9238473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:46:15+00:00 2026-06-18T07:46:15+00:00

In my question before, I mentioned, my OutputStream would do strange things. that is

  • 0

In my question before, I mentioned, my OutputStream would do strange things. that is past now, but suddenly a wild further problem appeared. I do not know, how I can read out my InputStream. I had it so far, I could read Byte for Byte, but that is not what i want. I want a variable length-readIn, I mean if 3 Bytes are send i just want to read 3 Bytes, if 9 Bytes are send I want to read 9 Bytes.

Here’s my Code:

package com.example.wettkampftimerbt;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URL;

import android.content.Context;
import android.os.AsyncTask;
import android.os.StrictMode;
import android.util.Log;

public class network extends AsyncTask<URL, String, String> {

    private final String PREFS_NAME = "prefs", W2 = "w2", W3 = "w3", W4 = "w4",
            W5 = "w5", W6 = "w6", SENDBEG = "sendbeg", SENDEND = "sendend";
    int w1, w2, w3, w4, w5, w6, w7, w8, sendend, sendbeg, diny1, diny2, diny3,
            diny4, diny5, diny6, diny7, diny8, dinymin, dinysek, w90, w91, w92,
            w93, w94, w95, w96, w97, netpru, y, retries, length, btw1 = 0,
            btw2 = 0, btw3 = 0, btw4 = 0, btw5 = 0, btw6 = 0, btw7 = 0,
            btw8 = 0, btw9 = 0, btw10 = 0;
    boolean visi1, visi2, visi3, visi4, visi5, visi6, visi7, visi8, login1,
            login2, login3, login4, login5, login6, login7, login8, data;
    Context context;
    private final String LFDNR1 = "lfdnr1";
    private final String LFDNR2 = "lfdnr2";
    private final String LFDNR3 = "lfdnr3";
    private final String LFDNR4 = "lfdnr4";
    private final String LFDNR5 = "lfdnr5";
    private final String LFDNR6 = "lfdnr6";
    private final String LFDNR7 = "lfdnr7";
    private final String LFDNR8 = "lfdnr8", COMMAND = "command", LANE = "lane",
            DINYMIN = "dinymin", DINYSEK = "dinysek";
    Socket sock;
    byte[] btw;
    byte[] bta;




    protected String doInBackground(URL... params) {
        retries = 1;
        try {
            getPrefs(context);
            btw1 = sendbeg;
            btw2 = w2;// to
            btw3 = w3;// from
            btw4 = w4;// lfdnr
            btw5 = w5;// command
            visi(context, false);
            SocketAddress sockaddr = new InetSocketAddress("192.168.0.7", 2001);
            sock = new Socket();
            int timeout = 1000; // 1000 millis = 1 second
            sock.connect(sockaddr, timeout);
            sock.setReuseAddress(true);
            System.out.println(sock);

                        //from here

        if (sock.isConnected()) {
            DataOutputStream dos = new DataOutputStream(
                    (OutputStream) sock.getOutputStream());
            DataInputStream dis = new DataInputStream(sock.getInputStream());
            BufferedReader rdr = new BufferedReader(new InputStreamReader(dis));
            getByte((byte) btw1, (byte) btw2, (byte) btw3, (byte) btw4,
                    (byte) btw5, (byte) btw6, (byte) btw7, (byte) btw8,
                    (byte) btw9, (byte) btw10);
            dos.write(btw, 0, length); 
            int in = rdr.read();
            dos.flush();
            dos.close();
            dis.close();

                                 //to here edited

                System.out.printf("dis: ", btw);
                visi(context, true);
                if (diny5 != 32)
                    System.out.println("ungleich");
                reccom();
                sock.close();
            }
        } catch (Exception e) {
            e.printStackTrace();

            System.out.println("IO error " + e + " lulu");
        }

        return "Done";
    }

    private byte[] getByte(byte byte1, byte byte2, byte byte3, byte byte4,
            byte byte5, byte byte6, byte byte7, byte byte8, byte byte9,
            byte byte10) {
        if (byte10 > 0) {
            btw = new byte[] { byte1, byte2, byte3, byte4, byte5, byte6, byte7,
                    byte8, byte9, byte10 };
            length = 10;
        } else if (byte9 > 0 && byte10 == 0) {
            btw = new byte[] { byte1, byte2, byte3, byte4, byte5, byte6, byte7,
                    byte8, byte9 };
            length = 9;
        } else if (byte8 > 0 && byte9 == 0 && byte10 == 0) {
            btw = new byte[] { byte1, byte2, byte3, byte4, byte5, byte6, byte7,
                    byte8 };
            length = 8;
        } else if (byte7 > 0 && byte8 == 0 && byte9 == 0 && byte10 == 0) {
            btw = new byte[] { byte1, byte2, byte3, byte4, byte5, byte6, byte7 };
            length = 7;
        } else if (byte6 > 0 && byte7 == 0 && byte8 == 0 && byte9 == 0
                && byte10 == 0) {
            btw = new byte[] { byte1, byte2, byte3, byte4, byte5, byte6 };
            length = 6;
        } else if (byte5 > 0 && byte6 == 0 && byte7 == 0 && byte8 == 0
                && byte9 == 0 && byte10 == 0) {
            btw = new byte[] { byte1, byte2, byte3, byte4, byte5 };
            length = 5;
        } else if (byte4 > 0 && byte5 == 0 && byte6 == 0 && byte7 == 0
                && byte8 == 0 && byte9 == 0 && byte10 == 0) {
            btw = new byte[] { byte1, byte2, byte3, byte4 };
            length = 4;
        } else if (byte3 > 0 && byte4 == 0 && byte5 == 0 && byte6 == 0
                && byte7 == 0 && byte8 == 0 && byte9 == 0 && byte10 == 0) {
            btw = new byte[] { byte1, byte2, byte3 };
            length = 3;
        } else if (byte2 > 0 && byte3 == 0 && byte4 == 0 && byte5 == 0
                && byte6 == 0 && byte7 == 0 && byte8 == 0 && byte9 == 0
                && byte10 == 0) {
            btw = new byte[] { byte1, byte2 };
            length = 2;
        } else if (byte1 > 0 && byte2 == 0 && byte3 == 0 && byte4 == 0
                && byte5 == 0 && byte6 == 0 && byte7 == 0 && byte8 == 0
                && byte9 == 0 && byte10 == 0) {
            btw = new byte[] { byte1 };
            length = 1;
        } else if (byte1 == 0 && byte2 == 0 && byte3 == 0 && byte4 == 0
                && byte5 == 0 && byte6 == 0 && byte7 == 0 && byte8 == 0
                && byte9 == 0 && byte10 == 0) {
            btw = new byte[] {};
            length = 0;
        }
        return btw;
    }


    public void onPreExecute(Context context) {
        System.out.println("network_onPreExecute");
        getPrefs(context);
    }

    public void getPrefs(Context context) {
        w2 = 0;
        w3 = 0;
        w4 = 0;
        w5 = 0;
        w6 = 0;
        System.out.println("network_getPrefs");
        System.out.println(context);
        w2 = Integer.valueOf(context.getSharedPreferences(PREFS_NAME,
                Context.MODE_PRIVATE).getString(W2, "03"));
        context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().remove(W2).commit();
        w3 = Integer.valueOf(context.getSharedPreferences(PREFS_NAME,
                Context.MODE_PRIVATE).getString(W3, "00"));
        context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().remove(W3).commit();
        w4 = Integer.valueOf(context.getSharedPreferences(PREFS_NAME,
                Context.MODE_PRIVATE).getString(W4, "00"));
        context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().remove(W4).commit();;
        w5 = Integer.valueOf(context.getSharedPreferences(PREFS_NAME,
                Context.MODE_PRIVATE).getString(W5, "49"));
        context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().remove(W5).commit();
        w6 = Integer.valueOf(context.getSharedPreferences(PREFS_NAME,
                Context.MODE_PRIVATE).getString(W6, "00"));
        context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().remove(W6).commit();
        sendbeg = Integer.valueOf(context.getSharedPreferences(PREFS_NAME,
                Context.MODE_PRIVATE).getString(SENDBEG, "00"));
        context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().remove(SENDBEG).commit();
        sendend = Integer.valueOf(context.getSharedPreferences(PREFS_NAME,
                Context.MODE_PRIVATE).getString(SENDEND, "00"));
        context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().remove(SENDEND).commit();
    }

    public network(Context context) {
        System.out.println("network_Context");
        onPreExecute(context);
        this.context = context;
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
}

Im even thankful for code optimizing-tips.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:46:16+00:00Added an answer on June 18, 2026 at 7:46 am
    try(FileInputStream in = new FileInputStream(accountFile);
        BufferedReader rdr = new BufferedReader(new InputStreamReader(in))
       )
    {
        String in = rdr.readLine();
    }
    

    this is part of my project what is doing exactly what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a similar question before, but this is a few steps beyond that
Before this question is blocked for duplication, I would like to mention is that
This has been asked before (question no. 308581) , but that particular question and
Before I ask my question, I want to mention that I am only today
I asked a similar question before, but it was answered inadequately so I thought
I have asked this question before but did not get the satisfied answer as
I have asked this question before and the problem was half solved in the
I asked a similar question before only to later discover that what I thought
I've asked very similar question before and should have mentioned more detailed. Last time
I have asked a similar question before but this time I want to focus

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.