1

If I use int oneByte = dis.read(byteArray, 0, 1) does this mean I am reading only 1 byte and I am assigning its decimal value to integer oneByte?

If I want to check for | (pipe) character to break out of a loop can I do something like this: while((oneByte = dis.read(byteArray, 0, 1)) != 124)

1

6 Answers 6

7

If I use int oneByte = dis.read(byteArray, 0, 1) does this mean I am reading only 1 byte and I am assigning its decimal value to integer oneByte?

Nope. You're reading up to 1 bytes into byteArray and receiving the number of bytes read in oneByte. Perhaps you'd prefer:

int oneByte = dis.read();

Also be careful because you'll get the integer value...not a decimal. Keep in mind that it will return -1 when you reach the end of the stream.

If I want to check for | (pipe) character to break out of a loop can I do something like this: while((oneByte = dis.read(byteArray, 0, 1)) != 124)

You'll need to also check for the end of stream (-1). Try something more like this:

while(true) {
    int oneByte = dis.read();
    if(oneByte == -1 || oneByte == '|') {
        break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

No, it means you are trying to read 1 byte and you are assigning the number of bytes actually read to oneByte. So oneByte cannot be greater than 1 in this case. If you want to check for the "|" char you have to do:

dis.read(byteArray, 0, 1);
while(byteArray[0] != 124) {
    dis.read(byteArray, 0, 1);
}

Comments

0

As Matthew says in a comment, you should read the API.

The read() method is overloaded. Without arguments, it does what you want. With the arguments you're passing, it returns the number of characters read.

Make sure you check for EOF (-1) too.

Comments

0

No. oneByte = dis.read(byteArray,0,1); The 0 means you're reading from an offset of 0 (so if you haven't read anything from the stream yet this will be the beginning of the stream), the 1 means you want to read up to 1 byte, the byteArray is the array into which you're reading and the return value assigned to oneByte is the number of bytes read (since if your stream contains fewer bytes than you tried to read this number would be less than the len parameter).

You can break on pipe by using the read() method:

while(dis.read() !- 124)

Comments

0

dis.read(buffer) will return as result the numbers of bytes read.
dis.read() will read ONE single byte and return it's value as integer (0 to 255)

The while loop should be like this to do what you want:
while ((oneByte = dis.read()) != 124)

Comments

0

If you check the Java API for InputStream, you'll see that the 'return' value for InputStream.read(byte[], int, int) is described as:

the total number of bytes read into the buffer, or -1 if there is
no more data because the end of the stream has been reached.

So, no. You're reading one byte and storing into byteArray[0]. OneByte will be -1, 0 or 1. Breaking your loop will not work in this scenario. But, for the record, if you really only need one byte at a time, the InputStream.read() method will do the trick.

You also, for readability's sake, may want to check against the exact character you're looking for. So:

while (stream.read()!='|'){ 
    //stuff
}

This way, anyone who reads your code (future coders, graders, etc.) will immediately know "Oh, it breaks on a Pipe Character".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.