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

How to get length of bufferedinputstream  RSS feed

 
Mary Cole
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How to get length of buffered inputstream.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Length? You mean the number of bytes you would read if you read to the end of the stream? If that's what you mean, then you get it by reading to the end of the stream and counting the bytes.
 
Mary Cole
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes the number of bytes....is there a sample program i can refer to





Am always getting sizenew as 16 and the file is never written in /tmp... pls advice
[ March 31, 2008: Message edited by: Mary Cole ]
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, your question is "How to get the length of a BufferedInputStream?".

However, the sole question implies more questions. First, is the underlying stream linked to a file. Is that what you want to know, the size of the underlying file?

I ask this because a BufferedInputStream might just as well be working with a socket, piped stream or any other kind of stream that could make this question irrelevant.

Now, if what you want to know is the size of the underlaying file, and you now longer have a reference to it, you just have the reference to the BufferedInputStream, then you will have to read all the bytes to reach the end of the stream in order to determine its size.

Another interpretation for your question could be that you would like to know the number of available bytes already in the buffer. In whose case you could use the method "available()" in the BufferedInputStream.

Now, in your example, your are writing the bytes to temp file. If you intend just to know the size, why do you not sum the length value return by the read method in every loop instead of writing the output to a ByteArrayOutputStream?

Hope this helps!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mary: it looks like you are getting the size correctly, but you need to close the FileOutputStream to make sure it's written correctly.
[ March 31, 2008: Message edited by: Jim Yingst ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!