Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

problems with reading input stream  RSS feed

 
Constanze Michaelis
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi @ll

I have an Input Stream that I read out with BufferdInputStream. after reading out High Byte I shift left. Reading out Low byte gives in 5 of 256 an irregular value.


How can I solve?

regards
Constanze
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there's no high byte or low byte:

public abstract int read() throws IOException

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255.


java.io.InputStream
 
Constanze Michaelis
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i get the data from a microcontroller, and this sends first the high byte and than the low byte. and this low byte is via ethernet in 5 of 256 terms wrong.

regards
Constanze
[ January 24, 2005: Message edited by: Constanze Michaelis ]
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

public abstract int read() throws IOException

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255.

Given a requirement like the one you've stated and a method signiture like the above, I'd read once to get the high byte and again to get the low byte.
 
Constanze Michaelis
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi joe

I know, but I get values above 256, why?

bye

Constanze
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because a 2-byte memory space can hold 65536 different values. When you do that shift-left of the "help" variable you are essentially multiplying the value of help (possibly 255) by 256.
 
Constanze Michaelis
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, i got values over 256 not with the value, i get it with help.read() ...

But i think i found out the problem, otherwise i will write again tomorrow.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your first problem is that you are not reading from an InputStream but rather a BufferedReader that wraps an InputStreamReader that wraps an InputStream. Note the JavaDocs for read() in BufferedReader:
Read a single character.

Returns: The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached
If you're dealing with bytes, you should use a BufferedInputStream instead.
Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.
Note that in both cases -1 can be returned when the stream ends.

Another thing, when combining bytes, it's better to use | (bitwise or) instead of addition.Finally, if you're trying to create a signed 16-bit value, you'll want to put this nito a short -- not an int. Integers are 32 bits (4 bytes), so 0xFFFF which you want as -1 will be 65535.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!