Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To replace StringBufferInputStream with StringReader in Java, use StringReader when the consuming API accepts a Reader. If the API requires an InputStream, encode the string with the required charset and wrap the resulting bytes in ByteArrayInputStream instead. The classes are not type-compatible.

StringBufferInputStream has been deprecated since Java 1.1 because it treats characters as bytes and keeps only each character’s low eight bits. That behavior can corrupt accented characters, currency symbols, CJK text, emoji, and other characters that need proper encoding. The official API documentation identifies StringReader as the preferred replacement for character-oriented reading.

Key takeaways

  • StringReader is the correct replacement when the next API accepts a Reader or another character-oriented input.
  • StringReader is not a drop-in replacement for a parameter or variable typed as InputStream.
  • StringBufferInputStream has been deprecated since Java 1.1 because it uses only the low eight bits of each Java character instead of applying a real character encoding.
  • For an API that requires bytes, use ByteArrayInputStream with an explicitly selected charset such as StandardCharsets.UTF_8.
  • For arbitrary binary data, keep the data in a byte[]; do not represent binary content as a String.
  • Reader.of(CharSequence) is a newer alternative documented in Java SE 26, but new StringReader(text) remains the broadly compatible choice for older Java targets.

Why is StringBufferInputStream deprecated?

StringBufferInputStream extends InputStream, but it does not convert a Java string into bytes using UTF-8, UTF-16, ISO-8859-1, or any other defined character encoding. According to the StringBufferInputStream Java SE API documentation, the class uses only the low eight bits of each character. The same documentation recommends StringReader when an application wants to read characters from a string.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For example:

String text = "é € 世界";
InputStream legacy = new StringBufferInputStream(text);

The resulting stream does not contain a correctly encoded representation of the original text. Characters that do not fit in the low eight bits can be truncated or otherwise corrupted. ASCII-only tests often conceal the problem because ordinary ASCII characters fit within that range.

The deprecation does not mean that the class was removed. The class remains documented in the Java SE 26 API. The important issue is not merely the compiler warning; the class has the wrong data-conversion behavior for modern text processing.

Is StringReader a direct replacement for StringBufferInputStream?

No, not in every context. StringBufferInputStream is an InputStream, while StringReader is a Reader. The correct migration depends on the abstraction required by the next method, field, parser, or library.

Class Abstraction Reads Use it when
StringBufferInputStream InputStream Bytes derived incorrectly from characters Legacy code only; generally replace it
StringReader Reader Characters from a String The consumer processes text or accepts Reader
ByteArrayInputStream InputStream Bytes from a byte[] The consumer genuinely requires bytes
InputStreamReader Reader Characters decoded from bytes You start with an encoded byte stream and need text

The StringReader Java SE 26 API describes a character stream whose source is a string. Its read() methods return character data, not byte data. Changing from InputStream to Reader may therefore require changes to declarations, method signatures, buffers, counters, and downstream logic.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do you make the basic StringReader replacement?

If the consuming code is character-oriented, replace the deprecated construction with new StringReader(text):

// Before
String text = "config=true";
StringBufferInputStream input =
    new StringBufferInputStream(text);

// After
String text = "config=true";
Reader reader = new StringReader(text);

A resource-safe version is:

String text = "Hello, 世界";

try (Reader reader = new StringReader(text)) {
    // Pass reader to a character-oriented API
}

StringReader is backed by an in-memory string, so closing it does not release a file, socket, or other operating-system resource. However, StringReader still follows the Reader lifecycle contract. After the reader is closed, further read-related operations can throw IOException. Close the reader according to the ownership conventions of the surrounding API, and do not close a reader that its caller still expects to use.

How do method calls change from InputStream to Reader?

The method names can look similar, but the data types and meanings change when code moves from a byte stream to a character stream.

Single-character reads

// Before
InputStream input = new StringBufferInputStream(text);
int value = input.read();

// After
Reader reader = new StringReader(text);
int value = reader.read();

Both methods return int and use -1 for end of input, but they do not return the same kind of value. InputStream.read() returns a byte value from 0 through 255, or -1. Reader.read() returns a character value, or -1. The old class obtains its value from the low eight bits of the next character, whereas StringReader reads the string’s character data. The StringBufferInputStream API and StringReader API document these different stream contracts.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Array reads

Byte buffers must become character buffers when the receiving logic is changed to use a reader:

// Before
byte[] buffer = new byte[1024];
int count = input.read(buffer);

// After
char[] buffer = new char[1024];
int count = reader.read(buffer);

Review every use of count, buffer offset, terminator, checksum, and conversion operation. A byte count and a character count are not interchangeable.

Method parameters and fields

Change the consuming API when the operation is genuinely text-based:

void parse(Reader source) throws IOException {
    // Parse character data
}

parse(new StringReader(text));

If the API can accept a String directly, passing the string directly is often clearer than creating a reader. A stream or reader should represent a real interface requirement, not remain merely because the deprecated implementation used one.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What should you use when the method requires InputStream?

When a downstream method requires InputStream, do not pass a StringReader and do not attempt a cast. Encode the string into bytes using the charset required by the protocol, file format, or receiving API, then wrap those bytes in ByteArrayInputStream.

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

String text = "Hello, 世界";

try (InputStream input = new ByteArrayInputStream(
        text.getBytes(StandardCharsets.UTF_8))) {
    // Pass input to an API that requires InputStream
}

The ByteArrayInputStream Java SE 26 API documents the class as an in-memory byte stream backed by a byte array. This approach preserves a deliberate byte representation instead of reproducing the deprecated class’s accidental truncation.

This assignment is invalid:

InputStream input = new StringReader(text); // Does not compile

Use one of these types instead:

Reader reader = new StringReader(text);

InputStream input = new ByteArrayInputStream(
    text.getBytes(StandardCharsets.UTF_8));

Which charset should you use?

Use the charset defined by the receiving protocol, file format, or API. UTF-8 is appropriate when the data contract says UTF-8, but UTF-8 is not universally correct merely because the source value is a Java String.

For explicitly UTF-8 data:

byte[] encoded = text.getBytes(StandardCharsets.UTF_8);
InputStream input = new ByteArrayInputStream(encoded);

Avoid this migration unless platform-default behavior is intentionally part of the design:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
byte[] encoded = text.getBytes();

Explicit charset handling is especially important when code runs on different operating systems or moves between JDK generations. The Oracle JDK Migration Guide describes charset-default migration considerations, including the distinction between many UTF-8 defaults in JDK 18 and later and the platform-dependent behavior common in JDK 17 and earlier. Existing code should not rely on that historical difference.

How do you convert encoded bytes back into characters?

Use InputStreamReader when the starting point is an encoded byte stream and the consuming code needs a Reader. Supply the same charset that was used to encode the bytes.

import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;

Reader reader = new InputStreamReader(
    input,
    StandardCharsets.UTF_8
);

The InputStreamReader Java SE 26 API defines InputStreamReader as the bridge from byte streams to character streams. Do not encode with UTF-8 and later decode with ISO-8859-1 unless intentional transcoding is taking place; the encoding and decoding charsets must agree.

InputStreamReader may read ahead from its underlying byte stream. Code should therefore avoid mixing direct reads from the original InputStream with reads through the wrapper.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do you read lines from a String?

Wrap StringReader in BufferedReader when the parser needs convenient line-oriented operations such as readLine():

try (BufferedReader reader =
         new BufferedReader(new StringReader(text))) {

    String line;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
}

BufferedReader is useful for line handling, although the underlying content is already in memory. If the receiving API accepts a String or a collection of lines directly, using that API may be simpler than introducing a reader.

When should you preserve bytes instead of using a Reader?

Use a byte stream when byte boundaries matter or when the consumer handles binary or encoded data rather than abstract text. Do not replace an InputStream with a Reader for compressed data, cryptographic material, binary serialization, images, media, checksums, signatures, protocol frames, or any format whose API is explicitly byte-oriented.

Input data and consumer Recommended representation Why
Text API accepts Reader new StringReader(text) Preserves character data without an unnecessary encoding step
Text must be supplied as encoded bytes new ByteArrayInputStream(text.getBytes(charset)) Creates a defined byte representation
Binary data is already available byte[] plus ByteArrayInputStream Avoids treating arbitrary bytes as text
Encoded bytes must become text InputStreamReader(input, charset) Decodes bytes using an explicit charset
API accepts String Pass the string directly Removes an unnecessary stream abstraction

What if the old code depended on StringBufferInputStream’s broken behavior?

Some legacy code may have implicitly relied on the deprecated class returning the low eight bits of each character. That possibility must be investigated before changing byte output. Replacing the old class with UTF-8 changes the resulting bytes, even if the new behavior is the correct behavior for text.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If the intended data model is specifically ISO-8859-1, name that encoding explicitly:

InputStream input = new ByteArrayInputStream(
    text.getBytes(StandardCharsets.ISO_8859_1)
);

ISO-8859-1 is appropriate only when the data contract requires it. It is not a general compatibility substitute for every old use of StringBufferInputStream.

If the data is truly arbitrary binary data, retain the original bytes:

byte[] data = ...;

try (InputStream input = new ByteArrayInputStream(data)) {
    // Process binary data
}

Do not use a String as a storage format for arbitrary binary content. A compatibility implementation that deliberately reproduces low-eight-bit truncation should be used only after confirming that downstream behavior requires it and documenting the decision.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do character counts differ from byte counts?

Java string length and encoded byte length measure different things:

int javaLength = text.length();
int utf8ByteLength = text.getBytes(StandardCharsets.UTF_8).length;

String.length() measures UTF-16 code units, while the second expression measures bytes in the selected UTF-8 encoding. Unicode supplementary characters such as emoji can occupy two UTF-16 code units, and non-ASCII text can occupy more than one UTF-8 byte per encoded character. Review offsets, framing, allocation, checksums, and protocol lengths whenever a migration changes byte processing to character processing or vice versa.

How should you test the migration?

Test the data boundary rather than validating only that the code compiles. A useful test string includes several character categories:

String text = "ASCII, café, €, 世界, 😀";
  • Plain ASCII characters.
  • Accented Latin characters such as é.
  • Currency symbols such as €.
  • CJK characters such as 世界.
  • Supplementary characters such as 😀.
  • An empty string.
  • Embedded line endings, including the line-ending forms relevant to the input contract.

For a Reader migration, verify that the consumed character content equals the original string. For a byte-oriented migration, verify bytes against the required charset and then verify that the receiving side decodes them with that same charset. Add assertions for byte length where protocol framing, file formats, hashing, checksums, or signatures depend on byte counts.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What is a safe migration procedure?

  1. Find every construction and import of StringBufferInputStream.
  2. Inspect the next method, field, or library boundary and identify whether it expects Reader, InputStream, String, or another type.
  3. If the consumer expects Reader, replace construction with new StringReader(text).
  4. Change byte-oriented declarations and buffers, such as InputStream to Reader and byte[] to char[], then review the associated logic.
  5. If the consumer expects InputStream, identify the required charset and use ByteArrayInputStream(text.getBytes(charset)).
  6. If bytes are later decoded, use InputStreamReader(input, charset) with the matching charset.
  7. Check whether the code assumes that character counts equal byte counts.
  8. Review serialization, checksums, hashing, signatures, protocol framing, and file-format behavior.
  9. Test ASCII, accented text, currency symbols, CJK characters, emoji, empty input, and line endings.
  10. Compile with deprecation warnings enabled and remove the deprecated import.

For a direct Java compilation, use:

javac -Xlint:deprecation -Xlint:unchecked YourClass.java

Maven warning configuration varies with the project’s compiler-plugin version and existing build configuration, so update the project’s current compiler settings rather than copying a supposedly universal configuration.

What modern alternatives are available?

Reader.of(CharSequence)

Java SE 26 documents Reader.of(CharSequence) as an alternative that can be more efficient than StringReader for reading from a CharSequence. The API is also useful when the source is not specifically a String:

Reader reader = Reader.of(text);

Use Reader.of(CharSequence) only when the application targets a Java release that provides it. For libraries and applications supporting older Java releases, retain:

Reader reader = new StringReader(text);

The StringReader Java SE 26 documentation provides the relevant API note and version context. The documentation does not establish that Reader.of is faster in every situation, so treat its efficiency note as conditional rather than as a universal benchmark claim.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Decision rule

Reader API      -> StringReader
InputStream API -> ByteArrayInputStream + explicit charset
Binary data     -> byte[] + ByteArrayInputStream
String API      -> pass the String directly when possible

The practical replacement for deprecated StringBufferInputStream is therefore determined by the consumer. Use StringReader to read text as characters, use an explicitly encoded ByteArrayInputStream when bytes are required, and preserve byte[] data when the content is binary.

Frequently Asked Questions

Can StringReader replace StringBufferInputStream without changing any other code?

No. StringReader is a Reader and StringBufferInputStream is an InputStream, so code that expects InputStream must be changed to use an explicitly encoded ByteArrayInputStream or another byte source. StringReader is a direct replacement only for character-oriented consumers.

What is the correct UTF-8 replacement for StringBufferInputStream?

Use new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)) when the receiving API requires InputStream. Use new StringReader(text) instead when the receiving API accepts Reader; UTF-8 is not needed for that character-to-character path.

Why does StringBufferInputStream corrupt Unicode text?

StringBufferInputStream uses only the low eight bits of each Java character rather than converting the string with a defined charset. Characters outside that range therefore cannot be represented as their proper UTF-8, UTF-16, or other encoded byte sequences.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Should binary data be converted to String before creating an InputStream?

No. Keep arbitrary binary data in a byte[] and wrap it with ByteArrayInputStream when an InputStream is needed. Converting binary data through String can lose or reinterpret byte values.

The Bottom Line

Bottom line: use StringReader for a Reader API, use ByteArrayInputStream with the required explicit charset for an InputStream API, and keep binary content as byte[]. Do not treat StringReader as a universal, type-compatible replacement for StringBufferInputStream.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply. Prices as of December 4, 2026 9:51 am GMT.