Questions tagged [bufferedinputstream]

An abstract mechanism for reading a data stream into a buffer, for improved reading performance

An BufferedInputStream is a wrapper around an InputStream, whereby the data being read is buffered locally. Buffering can be either fully-buffered or partially-buffered. Fully-buffered is where the entire contents of the InputStream is held locally in a variable. Partially-buffered is where a small portion of the stream data is held in a local variable, and as you read data from the buffer, those bytes are replaced with the next X bytes from the stream - ie the buffer overwrites itself as the data is consumed from it.

An InputStream is usually buffered to improve performance, as it allows data to be read from a network of file in large efficient chunks, rather than a number of smaller reads. This removes the overhead and performance impact of the multiple small reads. The downside, however, is that the buffer consumes some memory and has some CPU impact, but these downsides are usually very tiny, and are far outweighed by the benefits of buffering.

279 questions
106
votes
4 answers

Why does BufferedInputStream copy a field to a local variable rather than use the field directly

When I read the source code from java.io.BufferedInputStream.getInIfOpen(), I am confused about why it wrote code like this: /** * Check to make sure that underlying input stream has not been * nulled out due to close; if not return it; …
Saint
  • 1,328
  • 1
  • 6
  • 18
79
votes
7 answers

Download binary file from OKHTTP

I am using OKHTTP client for networking in my android application. This example shows how to upload binary file. I would like to know how to get inputstream of binary file downloading with OKHTTP client. Here is the listing of the example : public…
pratsJ
  • 3,059
  • 2
  • 19
  • 39
69
votes
5 answers

BufferedInputStream To String Conversion?

Possible Duplicate: In Java how do a read/convert an InputStream in to a string? Hi, I want to convert this BufferedInputStream into my string. How can I do this? BufferedInputStream in = new BufferedInputStream(sktClient.getInputStream()…
Harinder
  • 11,032
  • 14
  • 67
  • 124
37
votes
5 answers

Usage of BufferedInputStream

Let me preface this post with a single caution. I am a total beginner when it comes to Java. I have been programming PHP on and off for a while, but I was ready to make a desktop application, so I decided to go with Java for various reasons. The…
Jason Watkins
  • 1,585
  • 4
  • 17
  • 30
31
votes
4 answers

How do I peek at the first two bytes in an InputStream?

Should be pretty simple: I have an InputStream where I want to peek at (not read) the first two bytes, i.e. I want the "current position" of the InputStream to stil be at 0 after my peeking. What is the best and safest way to do this? Answer - As I…
Epaga
  • 35,261
  • 53
  • 143
  • 239
28
votes
4 answers

Should I buffer the InputStream or the InputStreamReader?

What are the differences (if any) between the following two buffering approaches? Reader r1 = new BufferedReader(new InputStreamReader(in, "UTF-8"), bufferSize); Reader r2 = new InputStreamReader(new BufferedInputStream(in, bufferSize), "UTF-8");
bdkosher
  • 5,238
  • 2
  • 29
  • 39
28
votes
3 answers

How to read all of Inputstream in Server Socket JAVA

I am using Java.net at one of my project. and I wrote a App Server that gets inputStream from a client. But some times my (buffered)InputStream can not get all of OutputStream that client sent to my server. How can I write a wait or some thing like…
26
votes
5 answers

Java BufferedReader back to the top of a text file?

I currently have 2 BufferedReaders initialized on the same text file. When I'm done reading the text file with the first BufferedReader, I use the second one to make another pass through the file from the top. Multiple passes through the same file…
T to the J
22
votes
2 answers

How to read a line in BufferedInputStream?

I am writing a code to read Input from user by using BufferedInputStream, But as BufferedInputStream reads the bytes my program only read first byte and prints it. Is there any way I can read/store/print the whole input ( which will Integer )…
PankajKushwaha
  • 732
  • 1
  • 8
  • 21
18
votes
5 answers

When I close a BufferedInputStream, is the underlying InputStream also closed?

InputStream in = SomeClass.getInputStream(...); BufferedInputStream bis = new BufferedInputStream(in); try { // read data from bis } finally { bis.close(); in.close(); } The javadoc for BufferedInputStream.close() doesn't mention…
Graham Borland
  • 57,578
  • 20
  • 131
  • 176
18
votes
4 answers

What are the default buffer size for java.io.BufferedInputStream on old and exotic JVMs?

I've been doing some research for a blog post regarding java.io.BufferedInputStream and buffers. Apparently, over the years, the default has grown from a measly 512 bytes to 8192 bytes as of (presumptuously) Sun's Java 7 implementation, and was…
Stu Thompson
  • 36,763
  • 19
  • 104
  • 155
18
votes
2 answers

Why is the performance of BufferedReader so much worse than BufferedInputStream?

I understand that using a BufferedReader (wrapping a FileReader) is going to be significantly slower than using a BufferedInputStream (wrapping a FileInputStream), because the raw bytes have to be converted to characters. But I don't understand why…
Andy King
  • 1,482
  • 1
  • 19
  • 28
16
votes
2 answers

Adding characters to beginning and end of InputStream in Java

I have an InputStream which I need to add characters to the beginning and end of, and should end up with another variable of type InputStream. How could I easily do this?
pqn
  • 1,494
  • 2
  • 22
  • 32
11
votes
5 answers

How java.io.Buffer* stream differs from normal streams?

1) How does buffered streams work in background, how do they differ from normal streams and what are the advantage(s) of using them? 2) DataInputStream is also Byte based. But it is having methods to readLine(). What's the point in here?
i2ijeya
  • 14,732
  • 16
  • 58
  • 72
10
votes
1 answer

Sockets: BufferedOutputStream or just OutputStream?

In order to get the fastest transfer speeds over TCP in Java, which is better: Option A: InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream(); Option B: BufferedInputStream in = new…
1
2 3
18 19