• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is bit sequence 0x00 not written to a FileOutputStream?  RSS feed

 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've been trying to understand how streams ( I/O streams ) handle byte data. I've been referring to texts from book 'Java I/O' by Elliotte Rusty Harold and I'm currently trying to understand the public int read() and public void write(int) methods of FileInputStream and FileOutputStream respectively.

I understand that the int argument of the write method internally converts the argument into unsigned byte by truncating the higher order bits and that the read method promotes the unsigned byte to an int before returning the value.

What is confusing is, is bit sequence 00000000 ( or any bit sequence that would become 0x00 after truncation of higher order bits ) not written to files? Or is it a sequence that is written but not read? When I say bit sequence 00000000, I mean any integer that has 8 zeroes in its right most 8 bits. This is because when I run the following test program,



I get the following output.

11111111111111111111111111111111
1000000000
100000100
0
11111111
255
100
4
11111111111111111111111111111111
-1
BUILD SUCCESSFUL (total time: 0 seconds)


So, is the bit sequence 0x00 not written to files?

Thanks for your help.
Chan.




 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chan Ag wrote:




Hint: How many reads are you doing per iteration? And how many iterations are there -- based on the data and the number of reads per iteration?

Henry
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with the output for 512 (0x0000_0200)? You get the output [1000000000], which appears correct to me. And you are getting [0] from 0x0000_0000. I think the problem is that toBinaryString does not pack the output from the left with 0s.

BTW: 512 = 0x0000_0200 not 0x000_0100.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chan Ag wrote:So, is the bit sequence 0x00 not written to files?

Sure it is. The problem is your extra read() on line 30.

Winston

Edit: Too slow, as usual.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. That second read was supposed to be commented. Such a bad, silly mistake. :-)

Thank you.
Chan.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell, thanks. Yeah, that would be 00000200.

Actually the value was 256 earlier and then I changed it to 512 but forgot to change the comments. :-)
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!