• Post Reply Bookmark Topic Watch Topic
  • New Topic

Number of bytes read from a Reader  RSS feed

 
Spencer Lee
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm interested in reading chars from a (Buffered)Reader. However, I'd like to know how much I've read in terms of bytes and not chars. I understand that the size of a char in bytes varies depending upon the charset... Is this something I'd have to compute given the char set? Or is there a way to retrieve this information from a Reader class or Stream class?

Thanks,
Spencer
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could override FileInputStream's read() methods to accumulate a byte total, use it to open the file and read that class using a InputStreamReader to do the character translation. If you wrap the InputStreamReader with a BufferedReader, be aware that the buffering will cause your byte count to be inconsistent with your character position.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can guarantee that the InputStreamReader doesn't do some buffering as well. It could very well grab a chunk of bytes and decode them at once - that's probably more efficient. A few years ago I had to get an exact count of how many bytes the characters I'd read represted. I ended up having to rewrite them through an OutputStreamWriter and count the bytes written. It seems a bit wasteful, but it works. Now if the encoding used is known to have a fixed length, you don't have to do this - you can just multiply the number of chars by 1 or 2 or whatever. But for something like UTF-8, you probably need to rewrite the chars as bytes to get an exact count.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!