Buffers?

I want to learn more about buffers and their use in Java and I was wondering if anyone wants to point me in a good direction? It should be specific to Java, and should explain what it means and why we wrap things like file readers in buffers.

(I'm sorta of clear on what a buffer is but that's about it, so a pretty basic tutorial would help.)

Thanks

[371 byte] By [mikebasa] at [2007-11-27 10:43:01]
# 1

Buffers are used for performance improvement.

Imagine a large stack of books on the floor and I tell you to take them down the hall and put them on a shelf.

There are two ways of doing this.

1) Pick up a book. Go down the hall. Put book on shelf. Repeat until finished.

2) Pick up a book. Put on wheeled book cart. Repeat until cart is full. Push cart down hall. Take books from cart and put on shelf. Repeat until finished.

In this analogy the cart is the buffer.

cotton.ma at 2007-7-28 19:22:04 > top of Java-index,Java Essentials,Java Programming...
# 2

That's what I thought, is there a more complete tutorial that you know of though? Like how to use them, when to use them, how to make them better etc etc... (I often have to deal with very large files so it would help to know for the future.)

mikebasa at 2007-7-28 19:22:04 > top of Java-index,Java Essentials,Java Programming...
# 3

> That's what I thought, is there a more complete

> tutorial that you know of though? Like how to use

> them, when to use them, how to make them better etc

> etc... (I often have to deal with very large files so

> it would help to know for the future.)

I think you're overthinking this.

See http://java.sun.com/docs/books/tutorial/essential/io/buffers.html

cotton.ma at 2007-7-28 19:22:04 > top of Java-index,Java Essentials,Java Programming...
# 4

heh, possibly :)

Thanks.

mikebasa at 2007-7-28 19:22:04 > top of Java-index,Java Essentials,Java Programming...
# 5

And a quick note of when to use them.

You should use a buffer everytime you use a stream. Whether that's a file stream, a socket stream or some other stream.

cotton.ma at 2007-7-28 19:22:04 > top of Java-index,Java Essentials,Java Programming...
# 6

I guess what I really wanted to do was learn more about I/O thanks for the link.

mikebasa at 2007-7-28 19:22:04 > top of Java-index,Java Essentials,Java Programming...
# 7

> And a quick note of when to use them.

>

> You should use a buffer everytime you use a stream.

> Whether that's a file stream, a socket stream or some

> other stream.

1. But not ByteArray[Input/Output]Stream :-)

2. Be aware of redundant buffering. I've seen code posted

in this form to copy a file that uses a BufferedInputStream, a BufferedOutputSteam,

and an array to copy between them. Just using the array in this case is

sufficient:

void bad(File inFile, File outFile) throws IOException {

BufferedInputStream in = new BufferedInputStream(new FileInputStream(inFile));

try {

bad(in, outFile);

} finally {

in.close();

}

}

void bad(BufferedInputStream in, File outFile) throws IOException {

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));

try {

copy(in, out);

} finally {

out.close();

}

}

void good(File inFile, File outFile) throws IOException {

InputStream in = new FileInputStream(inFile);

try {

good(in, outFile);

} finally {

in.close();

}

}

void good(InputStream in, File outFile) throws IOException {

OutputStream out = new FileOutputStream(outFile);

try {

copy(in, out);

} finally {

out.close();

}

}

void copy(InputStream in, OutputStream out) throws IOException {

byte[] buf = new byte[0x2000];

int len;

while((len = in.read(buf)) != 0) {

out.write(buf, 0, len);

}

}

BigDaddyLoveHandlesa at 2007-7-28 19:22:04 > top of Java-index,Java Essentials,Java Programming...