What is the resource efficient way to generate the input stream?

I am new to Java IO. I currently have these lines of code that generates an input stream based on a string.

String sb = new StringBuilder();
for(...){
   sb.append(...);
}
String finalString = sb.toString();
byte[] objectBytes = finalString.getBytes(StandardCharsets.UTF_8);
InputStream inputStream = new ByteArrayInputStream(objectBytes);

      

I may be misunderstanding something, but is there a better way to generate InputStream

from String other than using it getBytes()

?

For example, if the String is really large, 50 MB, and there is no way to create another copy (getBytes () for another 50 MB) due to resource constraints, this could potentially cause an out of memory error.

I just wanted to know if over lines of code is an efficient way to generate InputStream

from String. For example, is there a way that I can "pass" a String to an input stream without using extra memory? How is abstraction Reader

on top of String?

+3


source to share


3 answers


I think what you are looking for is StringReader , which is defined as:

A stream of characters sourced from a string.



To use this effectively, you need to know exactly where the bytes you want to read are. It supports both random and sequential access, so you can read all String

, char

on char

if you want.

+3


source


You are creating data, you are actually writing and want to use the data almost immediately, read.

The Unix method is to pipe the output of one process to the input of another process. Java also requires at least two threads. They will sync when produced and consumed.

PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream(in);
new Thread(() -> writeAllYouveGot(out)).start();
readAllYouveGot(in);

      



Here I started a topic to write with Runnable which calls some self-defined method on out

. new Thread

You might prefer ExecutorService instead of using .

I / O piping is rarely used, although the asynchronous behavior is optimal. It is even possible to set the pipe size in the PipedInputStream. The reason for this rare use is the need for a second thread.

To complete the work, one could probably wrap the binary inputs / outputs in new InputStreamReader(in, "UTF-8")

and new OutputStreamWriter(out, "UTF-8")

.

0


source


Try something like this (no promises about typos :)

BufferedReader reader = new BufferedRead(new InputStreamReader(yourInputStream), Charset.defaultCharset());
final char[] buffer = new char[8000];
int charsRead = 0;
while(true) {
    charsRead = reader.read(buffer, 0, 8000);
    if (charsRead == -1) {
        break;
    }
    // Do something with buffer
}

      

InputStreamReader converts from byte to char using Charset. BufferedReader allows you to read char blocks.

For really large streams of input, you can process the input in chunks rather than reading the entire stream into memory and then processing it.

-1


source







All Articles