cancel
Showing results for 
Search instead for 
Did you mean: 

JAVA Convert OutputStream to InputStream

former_member301120
Active Participant
0 Kudos

Hi to all,

I'm a newbie at java. I've written a user defined java mapping. I've got two actions within the mapping. First I call the execute method of an external mapping. This returns an outputstream. But the next method needs an inputstream. Exact here is my problem. How can an outputstream be converted into an inputstream.

Thanks for your help

Regards Christian

Accepted Solutions (0)

Answers (3)

Answers (3)

former_member301120
Active Participant
0 Kudos

Thanks for your help

former_member192892
Active Contributor
0 Kudos
Former Member
0 Kudos

Hi,

You can for instance use an InputStreamReader and a BufferedReader to read line by line the inputstream. Use this in conjunction with a while loop to read all the lines of the inputstream while at the same time writing the bytes of the current line in the while loop to the outputstream.

Something like the following sample code (not testet):

InputStreamReader isr = null;

BufferedReader buffReader = null;

byte[] bytes = null;

try {

isr = new InputStreamReader(is,"UTF-8");

buffReader = new BufferedReader(isr);

String currentLine;

if (buffReader.ready()) {

while ((currentLine = buffReader.readLine()) != null) {

bytes = currentLine.toString().getBytes();

os.write(bytes);

}

}

catch (IOException e) {

do something with your exception

} finally {

flush and close readers

}

Best Regards,

Daniel

Former Member
0 Kudos

whoops, just saw I didn't get u right the first time - you posed a question in west and I answered in east. But of cause this is SDN, thats how it sometimes works:-)

You could either use a ByteArrayInputStream and ByteArrayOutputStream to convert between the two. Otherwise use PipedInputStream and PipedOutputStream to do the conversion.

Best Regards,

Daniel