I am trying to read data from an InputStream, which can be either FileInputStream or ObjectInputStream. To achieve this, I wanted to clone the stream and try to read the object, and in case of an exception, convert the stream to a string using apache commons io.
PipedInputStream in = new PipedInputStream();
TeeInputStream tee = new TeeInputStream(stream, new PipedOutputStream(in));
Object body;
try {
ObjectInput ois = new ObjectInputStream(tee);
body = ois.readObject();
} catch (Exception e) {
try {
body = IOUtils.toString(in, Charset.forName("UTF-8"));
} catch (Exception e2) {
throw new MarshallerException("Could not convert inputStream");
}
}
Unfortunately, this does not work, because the program waits for incoming data when trying to convert a stream into a string.
Jan b source
share