use of org.mule.runtime.core.api.util.NotAnInputStreamException in project mule by mulesoft.
the class ByteArrayToObject method doTransform.
@Override
public Object doTransform(Object src, Charset encoding) throws TransformerException {
if (src instanceof byte[]) {
byte[] bytes = (byte[]) src;
if (this.checkStreamHeader(bytes[0])) {
return super.doTransform(src, encoding);
} else {
return new String(bytes, encoding);
}
} else {
try {
return ifInputStream(src, stream -> {
try {
PushbackInputStream pushbackStream = new PushbackInputStream(stream);
int firstByte = pushbackStream.read();
pushbackStream.unread((byte) firstByte);
if (this.checkStreamHeader((byte) firstByte)) {
return super.doTransform(pushbackStream, encoding);
} else {
try {
return org.apache.commons.io.IOUtils.toString(pushbackStream, encoding);
} finally {
// this also closes the underlying stream that's stored in src
pushbackStream.close();
}
}
} catch (IOException iox) {
throw new TransformerException(this, iox);
}
});
} catch (NotAnInputStreamException e) {
throw new TransformerException(transformOnObjectUnsupportedTypeOfEndpoint(this.getName(), src.getClass()));
}
}
}
Aggregations