Java Buffered Streams

Su Suriya Ravichandran Updated 18 Nov 2026
8 min read ·Lesson 157 of 306

What Buffering Means

A buffer is a block of memory placed between a program and a slow data source or destination. Instead of passing each small read or write straight to the operating system, the buffered stream collects data in memory and moves it in large blocks:

  • Buffered input: one large read from the source fills the buffer; many small read() calls are then served from memory.
  • Buffered output: many small write() calls fill the buffer; one large write sends it to the destination.

Every read or write that reaches the operating system (a system call) has a fixed overhead, regardless of size. Buffering turns millions of tiny system calls into a few hundred large ones.

Java has four buffered stream classes:

ClassWrapsAdds
BufferedInputStreamAn InputStreamBuffered byte reading, mark/reset support
BufferedOutputStreamAn OutputStreamBuffered byte writing
BufferedReaderA ReaderBuffered character reading, readLine() (BufferedReader lesson)
BufferedWriterA WriterBuffered character writing, newLine() (BufferedWriter lesson)

This lesson focuses on the two byte streams and on the buffering rules that apply to all four. The default buffer size is 8,192 bytes (or characters); a size can be passed to the constructor.

Measuring the Difference

import java.io.*;
import java.nio.file.*;

public class Speed {
    static long writeBytes(OutputStream out, int count) throws IOException {
        long start = System.nanoTime();
        for (int i = 0; i < count; i++) out.write(i);
        out.flush();
        return (System.nanoTime() - start) / 1_000_000;
    }

    static long readBytes(InputStream in) throws IOException {
        long start = System.nanoTime();
        while (in.read() != -1) { }
        return (System.nanoTime() - start) / 1_000_000;
    }

    public static void main(String[] args) throws IOException {
        int n = 2_000_000;
        try (OutputStream raw = new FileOutputStream("raw.bin")) {
            System.out.println("unbuffered write: " + writeBytes(raw, n) + " ms");
        }
        try (OutputStream buf = new BufferedOutputStream(new FileOutputStream("buffered.bin"))) {
            System.out.println("buffered write:   " + writeBytes(buf, n) + " ms");
        }
        try (InputStream raw = new FileInputStream("raw.bin")) {
            System.out.println("unbuffered read:  " + readBytes(raw) + " ms");
        }
        try (InputStream buf = new BufferedInputStream(new FileInputStream("buffered.bin"))) {
            System.out.println("buffered read:    " + readBytes(buf) + " ms");
        }
        System.out.println("files identical: " + (Files.mismatch(Path.of("raw.bin"), Path.of("buffered.bin")) == -1));
    }
}

Output from one run (times vary with hardware, operating system and JVM warm-up, but the ratio is consistently large):

unbuffered write: 657 ms
buffered write:   59 ms
unbuffered read:  472 ms
buffered read:    52 ms
files identical: true

Both versions write and read the same 2,000,000 bytes one at a time and produce identical files. The only difference is the buffer: FileOutputStream.write(int) and FileInputStream.read() make one system call per byte, while the buffered versions make one per 8 KB — about 245 calls instead of 2 million. That's why the buffered versions are roughly ten times faster here, and the gap grows with slower storage or network connections.

When Buffering Helps — and When It Doesn't

Buffering helps when code performs many small operations:

  • reading or writing single bytes or characters,
  • parsing a format field by field (DataInputStream.readInt() reads 4 bytes at a time),
  • writing output in small pieces as it's generated (rows, lines, tags),
  • object serialization, which makes many tiny writes.

It adds little when code already works in large blocks:

  • in.read(buffer) / out.write(buffer, 0, n) with an 8 KB or larger array,
  • transferTo, Files.copy, Files.readAllBytes, Files.readString, Files.write,
  • streams that are buffered already: System.in, Files.newBufferedReader/Writer, BufferedReader/BufferedWriter. Wrapping a buffered stream in another buffer just copies the data an extra time.

How BufferedOutputStream Decides When to Write

import java.io.*;
import java.nio.file.*;

public class Flush {
    public static void main(String[] args) throws IOException {
        Path p = Path.of("events.bin");
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(p.toFile()), 16);
        out.write(new byte[10]);
        System.out.println("after 10 bytes:  file has " + Files.size(p));
        out.write(new byte[10]);
        System.out.println("after 20 bytes:  file has " + Files.size(p) + "  (buffer of 16 overflowed)");
        out.write(new byte[100]);
        System.out.println("after 120 bytes: file has " + Files.size(p) + " (large write went straight through)");
        out.flush();
        System.out.println("after flush:     file has " + Files.size(p));
        out.close();
    }
}

Output:

after 10 bytes:  file has 0
after 20 bytes:  file has 10  (buffer of 16 overflowed)
after 120 bytes: file has 120 (large write went straight through)
after flush:     file has 120

With a deliberately tiny 16-byte buffer, the rules become visible:

  1. The first 10 bytes fit in the buffer, so nothing reaches the file yet.
  2. The next 10 don't fit (10 + 10 > 16). The buffer's existing 10 bytes are written to the file, then the new 10 bytes are stored in the now-empty buffer.
  3. A write at least as large as the buffer bypasses it. The buffer's 10 waiting bytes are written first (keeping the order correct), and then the 100 bytes are written directly — copying them through the buffer would only waste time.
  4. flush() writes whatever is still buffered (nothing here). close() flushes and then closes the file.

If the program ended without flush() or close(), any bytes still in the buffer would be lost — the file would be silently truncated. This is the main risk of buffered output, and why buffered streams always belong in try-with-resources.

mark and reset with BufferedInputStream

FileInputStream and socket streams can't go back. BufferedInputStream can, because the bytes are still in its buffer: mark(limit) remembers the current position, and reset() returns to it, as long as no more than limit bytes have been read since the mark.

import java.io.*;

public class MarkLimit {
    public static void main(String[] args) throws IOException {
        byte[] data = new byte[100];
        for (int i = 0; i < data.length; i++) data[i] = (byte) i;

        BufferedInputStream in = new BufferedInputStream(new ByteArrayInputStream(data), 8);
        in.mark(4);
        in.readNBytes(3);
        in.reset();
        System.out.println("reset within limit, next byte: " + in.read());

        in.mark(4);
        in.readNBytes(50);
        try {
            in.reset();
        } catch (IOException e) {
            System.out.println("reset after reading past the limit: " + e.getMessage());
        }
    }
}

Output:

reset within limit, next byte: 0
reset after reading past the limit: Resetting to invalid mark
  • Reading 3 bytes after mark(4) stays within the limit, so reset() goes back and the next byte is 0 again.
  • Reading 50 bytes after mark(4) exceeds the limit; the buffer has moved on, and reset() throws IOException. The buffer grows if needed to honour the mark limit, so choose a limit that covers what you actually read ahead — usually just a few bytes.

The main use is peeking: look at the first bytes of a stream to decide how to process it, then go back so the real reader sees the stream from the beginning.

Practical Example: Opening Plain or Compressed Logs Transparently

Log rotation compresses old log files (app.log.1.gz) while the current one stays plain (app.log). A tool that searches logs should handle both without the user caring which is which. GZIP data always starts with the bytes 1F 8B, so peeking at the first two bytes is enough to decide — but the GZIP decoder needs to read those same bytes again, which is exactly what mark/reset allow.

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.zip.*;

public class OpenLog {
    /** Opens a log file for reading, transparently decompressing it if it is gzip-compressed. */
    static BufferedReader openLog(Path file) throws IOException {
        InputStream raw = new BufferedInputStream(Files.newInputStream(file));
        try {
            raw.mark(2);
            int b1 = raw.read(), b2 = raw.read();
            raw.reset();
            boolean gzip = b1 == 0x1F && b2 == 0x8B;
            InputStream content = gzip ? new GZIPInputStream(raw) : raw;
            return new BufferedReader(new InputStreamReader(content, StandardCharsets.UTF_8));
        } catch (IOException | RuntimeException e) {
            raw.close();
            throw e;
        }
    }

    public static void main(String[] args) throws IOException {
        String log = "09:00 INFO started\n09:01 ERROR db timeout\n09:02 INFO recovered\n";
        Files.writeString(Path.of("app.log"), log);
        try (OutputStream gz = new GZIPOutputStream(Files.newOutputStream(Path.of("app.log.1.gz")))) {
            gz.write("08:00 INFO started\n08:30 ERROR disk full\n".getBytes(StandardCharsets.UTF_8));
        }
        for (String name : new String[] {"app.log", "app.log.1.gz"}) {
            try (BufferedReader r = openLog(Path.of(name))) {
                long errors = r.lines().filter(l -> l.contains(" ERROR ")).count();
                System.out.println(name + ": " + errors + " error(s)");
            }
        }
    }
}

Output:

app.log: 1 error(s)
app.log.1.gz: 1 error(s)

Step by step:

  1. BufferedInputStream makes peeking possible. The file stream itself can't go back, but the buffered wrapper keeps the first bytes in memory.
  2. Peek two bytes, then reset. mark(2) covers exactly the two bytes read. After reset(), the stream is back at the very beginning, so nothing is lost for the reader that follows.
  3. Decide by content, not by name. If the bytes are the GZIP signature, the stream is wrapped in a GZIPInputStream; otherwise it's used as it is. A renamed or misnamed file is still handled correctly. (The GZIPInputStream constructor reads the header immediately, and it can do so because the bytes were pushed back.)
  4. The rest of the chain is the same for both cases: InputStreamReader decodes UTF-8 and BufferedReader provides lines, so the calling code — counting ERROR lines — doesn't know or care whether the file was compressed.
  5. Closing on failure. If anything fails after the file was opened (for example a corrupt GZIP header), the catch block closes the file before rethrowing, so no file handle leaks. On success, ownership passes to the caller, whose try-with-resources closes the whole chain — closing the BufferedReader closes every stream beneath it.

Files.probeContentType couldn't do this reliably, since it usually looks only at the file name. Peeking at the content with a buffered stream is the robust technique, used by tools such as zgrep and by libraries that auto-detect image and archive formats.

Buffered Streams and Threads

BufferedInputStream and BufferedOutputStream are thread-safe in the sense that their methods are internally synchronized, so two threads writing to the same buffered stream won't corrupt its buffer. They don't make sequences of writes atomic, though: if two threads each write several pieces of a record, the pieces can interleave. Code that shares one output stream between threads should build each complete record first and write it with a single call, or coordinate the writes explicitly.

0 Comments

Reviewed before they appear

No comments yet.

Java
Java Introduction Java Features Java Syntax Java Comments Java Statements Java Output Java Variables Java Data Types Java Type Casting Java Operators Java Strings Java Numbers Java Math Java Booleans Java User Input Java Conditions Java If Else Java Switch Java While Loop Java For Loop Java Break Java Continue Java Arrays Java Methods Java Parameters Java Return Values Java Scope Java Recursion Install Java Install JDK Java IDEs Java JDK Java JRE Java JVM Java Compiler Java File Compile Java Run Java JShell Basics Java Packages Java OOP Java Classes Java Objects Class Methods Constructors Constructor Overload this Keyword static Keyword final Keyword Encapsulation Inheritance Polymorphism Abstraction Access Modifiers super Keyword Method Overload Method Override Instanceof Operator Nested Classes Inner Classes Anonymous Classes Enums Records Array Basics Array Length Loop Arrays Multidimensional Arrays Array Copy Array Sorting Array Search ArrayList LinkedList HashSet TreeSet HashMap TreeMap Queue Interface Deque Interface Stack Class Iterator List Interface Set Interface Map Interface Collections Class Comparable Comparator Generics Basics Generic Classes Generic Methods Wildcards String Basics String Length String Methods String Concatenation String Compare String Search String Replace String Split String Format StringBuilder StringBuffer Text Blocks Regular Expressions Number Types Integer Class Float Class Double Class BigInteger BigDecimal Math Class Random Numbers Number Format Date Basics LocalDate LocalTime LocalDateTime ZonedDateTime Date Formatting Date Parsing Period Class Duration Class Java Instant Class Java Exception Basics Java try...catch Java Multiple Catch Blocks Java finally Block Java throw Keyword Java throws Keyword Java Checked Exceptions Java Unchecked Exceptions Java Custom Exceptions Java try-with-resources Java Exception Chaining Java File Basics Java Create Files Java Read Files Java Write Files Java Delete Files Java File Paths Java File Class Java BufferedReader Java BufferedWriter Java FileInputStream Java FileOutputStream Java Object Streams Java Directory Handling Java NIO Basics Java Files API Java Input Java Scanner Class Java Console Input Java PrintStream Java InputStream Java OutputStream Java Reader Class Java Writer Class Java Buffered Streams Java Data Streams Java Generics Introduction Java Generic Types Java Generic Methods Java Bounded Types Java Wildcards Java Type Erasure Java Generic Interfaces Java Generic Collections Java Lambda Basics Java Lambda Syntax Java Functional Interfaces Java Method References Java Stream API Java Stream Creation Java Stream Filter Java Stream Map Java Stream Reduce Java Stream Sort Java Stream Collect Java Collectors Class Java Optional Class Java Parallel Streams Java Threads: Thread Basics Create a Thread in Java Java Thread Class Java Runnable Interface Java Thread States Java Thread Sleep Java Thread Join Java Thread Priority Java Synchronization Java Thread Safety Java Race Conditions Java Deadlocks Java Executor Service Java Thread Pools Java Callable Interface Java Future Interface Java CompletableFuture Java Concurrent Collections Java Atomic Classes Java Virtual Threads Java Networking Basics Java URL Class Java URI Class Java HttpClient Java HTTP Requests Java HTTP Responses Java Socket Basics Java ServerSocket Java DatagramSocket Java WebSocket Basics Java JDBC Basics Java JDBC Setup Java Database Connection Java JDBC Connection Interface Java JDBC Statement Java PreparedStatement Java ResultSet Java JDBC CRUD Operations Java JDBC Transactions Java JDBC Batch Update Java Connection Pooling with HikariCP Java JPA Basics: Entities, EntityManager and JPQL Java Hibernate Basics: SessionFactory, Session and HQL Package in Java: Basics Explained with Examples Create Package in Java: Step-by-Step Guide with Examples Import Package in Java: The import Statement Explained Access Control in Java: Packages, Classes and Members JAR File in Java: Create, Run and Inspect JAR Files Java Modules: Introduction to the Java Module System module-info in Java: Every Module Directive Explained Java Module Path: How --module-path Finds Your Modules Java Classpath: How -cp and CLASSPATH Find Your Classes Java Annotations: Basics of @ Metadata in Java Java Built-in Annotations: @Override, @Deprecated and More Java Custom Annotations: Create Your Own @interface Java Annotation Targets: @Target and ElementType Explained Java Reflection API: Inspect and Use Classes at Runtime Java Class Metadata: Names, Modifiers, Hierarchy and Generic Java Dynamic Objects: Create Objects Dynamically With Reflec Java Dynamic Proxy: Intercept Method Calls With Proxy and In Java Security Java Secure Coding Java Password Hashing Java Input Validation Java Cryptography Basics Java KeyStore Basics Java TLS Basics Java SecureRandom Java JUnit Basics Java JUnit Test Methods JUnit Assertions JUnit Test Fixtures JUnit Parameterized Tests Mockito in Java Java Mock Objects Java Integration Testing Java Code Coverage Maven in Java Create a Maven Project in Java Maven Dependencies Maven Plugins Gradle in Java Gradle Build Gradle Dependencies Build Executable JAR in Java Build WAR File in Java HTTP Basics in Java Java Servlet Basics Servlet Lifecycle in Java HttpServletRequest in Java HttpServletResponse in Java JSP in Java JSTL in JSP REST API in Java JSON in Java Java Web Application Spring Framework Basics Spring Boot Tutorial Spring MVC Spring Data JPA Spring Security Spring Boot REST API Jakarta EE Basics Java Deployment JAR Deployment Java WAR Deployment Run Java as a Service Docker Java: Dockerize a Java Application Java Environment Variables and Configuration Java Logging Basics Java Production Setup Java JVM Options Java JVM Monitoring Java Hello World App Java Calculator App Java Number Guess Game Java Student Manager Java Library System Java Bank App Java Todo App Java File Manager Java Quiz App Java REST API Project Java Chat Server Java Employee Manager Java E-commerce API
Ask about this post
AI Ask about this post

Ask questions about Java Buffered Streams and get answers drawn from it.

Signed-in readers only.