bytearraytoobject.java

来自「提供ESB 应用mule源代码 提供ESB 应用mule源代码」· Java 代码 · 共 97 行

JAVA
97
字号
/* * $Id: ByteArrayToObject.java 11427 2008-03-19 16:00:33Z tcarlson $ * -------------------------------------------------------------------------------------- * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com * * The software in this package is published under the terms of the CPAL v1.0 * license, a copy of which has been included with this distribution in the * LICENSE.txt file. */package org.mule.transformer.simple;import org.mule.api.transformer.TransformerException;import org.mule.config.i18n.CoreMessages;import org.mule.util.IOUtils;import java.io.IOException;import java.io.InputStream;import java.io.ObjectStreamConstants;import java.io.PushbackInputStream;import java.io.UnsupportedEncodingException;/** * <code>ByteArrayToObject</code> works in the same way as * <code>ByteArrayToSerializable</code> but checks if the byte array is a * serialised object and if not will return a String created from the bytes as the * returnType on the transformer. */public class ByteArrayToObject extends ByteArrayToSerializable{    // @Override    public Object doTransform(Object src, String encoding) throws TransformerException    {        if (src instanceof byte[])        {            byte[] bytes = (byte[])src;            if (this.checkStreamHeader(bytes[0]))            {                return super.doTransform(src, encoding);            }            else            {                try                {                    return new String(bytes, encoding);                }                catch (UnsupportedEncodingException e)                {                    throw new TransformerException(this, e);                }            }        }        else if (src instanceof InputStream)        {            try            {                PushbackInputStream pushbackStream = new PushbackInputStream((InputStream)src);                int firstByte = pushbackStream.read();                pushbackStream.unread((byte)firstByte);                                if (this.checkStreamHeader((byte)firstByte))                {                    return super.doTransform(pushbackStream, encoding);                }                else                {                    try                    {                        return 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);            }        }        else        {            throw new TransformerException(CoreMessages.transformOnObjectUnsupportedTypeOfEndpoint(                this.getName(), src.getClass(), endpoint));        }    }    private boolean checkStreamHeader(byte firstByte)    {        return (firstByte == (byte)((ObjectStreamConstants.STREAM_MAGIC >>> 8) & 0xFF));    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?