Search in sources :

Example 1 with NotAnInputStreamException

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()));
        }
    }
}
Also used : NotAnInputStreamException(org.mule.runtime.core.api.util.NotAnInputStreamException) PushbackInputStream(java.io.PushbackInputStream) IOException(java.io.IOException) CoreMessages.transformOnObjectUnsupportedTypeOfEndpoint(org.mule.runtime.core.api.config.i18n.CoreMessages.transformOnObjectUnsupportedTypeOfEndpoint) TransformerException(org.mule.runtime.core.api.transformer.TransformerException)

Aggregations

IOException (java.io.IOException)1 PushbackInputStream (java.io.PushbackInputStream)1 CoreMessages.transformOnObjectUnsupportedTypeOfEndpoint (org.mule.runtime.core.api.config.i18n.CoreMessages.transformOnObjectUnsupportedTypeOfEndpoint)1 TransformerException (org.mule.runtime.core.api.transformer.TransformerException)1 NotAnInputStreamException (org.mule.runtime.core.api.util.NotAnInputStreamException)1