• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to get length of bufferedinputstream

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

How to get length of buffered inputstream.
 
Marshal
Posts: 25832
69
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 ]
 
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!
 
Wanderer
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 no measure of health to be well adjusted to a profoundly sick society. -Krishnamurti Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic