⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 streammessageimpl.java

📁 实现了Jms的服务器源码,支持多种适配器,DB,FTP,支持多种数据库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/**
 * Redistribution and use of this software and associated documentation
 * ("Software"), with or without modification, are permitted provided
 * that the following conditions are met:
 *
 * 1. Redistributions of source code must retain copyright
 *    statements and notices.  Redistributions must also contain a
 *    copy of this document.
 *
 * 2. Redistributions in binary form must reproduce the
 *    above copyright notice, this list of conditions and the
 *    following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. The name "Exolab" must not be used to endorse or promote
 *    products derived from this Software without prior written
 *    permission of Exoffice Technologies.  For written permission,
 *    please contact info@exolab.org.
 *
 * 4. Products derived from this Software may not be called "Exolab"
 *    nor may "Exolab" appear in their names without prior written
 *    permission of Exoffice Technologies. Exolab is a registered
 *    trademark of Exoffice Technologies.
 *
 * 5. Due credit should be given to the Exolab Project
 *    (http://www.exolab.org/).
 *
 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Copyright 2000-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: StreamMessageImpl.java,v 1.13.2.1 2004/05/06 05:47:25 tanderson Exp $
 */
package org.exolab.jms.message;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import javax.jms.JMSException;
import javax.jms.MessageEOFException;
import javax.jms.MessageFormatException;
import javax.jms.MessageNotReadableException;
import javax.jms.MessageNotWriteableException;
import javax.jms.StreamMessage;


/**
 * This class implements the {@link javax.jms.StreamMessage} interface.
 * <p>
 * A StreamMessage is used to send a stream of Java primitives.
 * It is filled and read sequentially. It inherits from <code>Message</code>
 * and adds a stream message body. It's methods are based largely on those
 * found in <code>java.io.DataInputStream</code> and
 * <code>java.io.DataOutputStream</code>.
 * <p>
 * The primitive types can be read or written explicitly using methods
 * for each type. They may also be read or written generically as objects.
 * For instance, a call to <code>StreamMessage.writeInt(6)</code> is
 * equivalent to <code>StreamMessage.writeObject(new Integer(6))</code>.
 * Both forms are provided because the explicit form is convenient for
 * static programming and the object form is needed when types are not known
 * at compile time.
 * <p>
 * When the message is first created, and when {@link #clearBody}
 * is called, the body of the message is in write-only mode. After the
 * first call to {@link #reset} has been made, the message body is in
 * read-only mode. When a message has been sent, by definition, the
 * provider calls <code>reset</code> in order to read it's content, and
 * when a message has been received, the provider has called
 * <code>reset</code> so that the message body is in read-only mode for the
 * client.
 * <p>
 * If {@link #clearBody} is called on a message in read-only mode,
 * the message body is cleared and the message body is in write-only mode.
 * <p>
 * If a client attempts to read a message in write-only mode, a
 * MessageNotReadableException is thrown.
 * <p>
 * If a client attempts to write a message in read-only mode, a
 * MessageNotWriteableException is thrown.
 * <p>
 * Stream messages support the following conversion table. The marked cases
 * must be supported. The unmarked cases must throw a JMSException. The
 * String to primitive conversions may throw a runtime exception if the
 * primitives <code>valueOf()</code> method does not accept it as a valid
 * String representation of the primitive.
 * <p>
 * A value written as the row type can be read as the column type.
 *
 * <pre>
 * |        | boolean byte short char int long float double String byte[]
 * |----------------------------------------------------------------------
 * |boolean |    X                                            X
 * |byte    |          X     X         X   X                  X
 * |short   |                X         X   X                  X
 * |char    |                     X                           X
 * |int     |                          X   X                  X
 * |long    |                              X                  X
 * |float   |                                    X     X      X
 * |double  |                                          X      X
 * |String  |    X     X     X         X   X     X     X      X
 * |byte[]  |                                                        X
 * |----------------------------------------------------------------------
 * </pre>
 * <p>
 * Attempting to read a null value as a Java primitive type must be treated
 * as calling the primitive's corresponding <code>valueOf(String)</code>
 * conversion method with a null value. Since char does not support a String
 * conversion, attempting to read a null value as a char must throw
 * NullPointerException.
 *
 * @version     $Revision: 1.13.2.1 $ $Date: 2004/05/06 05:47:25 $
 * @author      <a href="mailto:mourikis@intalio.com">Jim Mourikis</a>
 * @author      <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
 * @see         javax.jms.StreamMessage
 */
public final class StreamMessageImpl extends MessageImpl
    implements StreamMessage {

    /**
     * Object version no. for serialization
     */
    static final long serialVersionUID = 2;

    /**
     * Type codes
     */
    private static final byte NULL = 0;
    private static final byte BOOLEAN = 1;
    private static final byte BYTE = 2;
    private static final byte BYTE_ARRAY = 3;
    private static final byte SHORT = 4;
    private static final byte CHAR = 5;
    private static final byte INT = 6;
    private static final byte LONG = 7;
    private static final byte FLOAT = 8;
    private static final byte DOUBLE = 9;
    private static final byte STRING = 10;

    /**
     * String values representing the above type codes, for error reporting
     * purposes
     */
    private static final String[] TYPE_NAMES = {
        "null", "boolean", "byte", "byte[]", "short", "char", "int", "long",
        "float", "double", "String"};

    /**
     * Empty byte array for initialisation purposes
     */
    private static final byte[] EMPTY = new byte[]{};

    /**
     * The byte stream to store data
     */
    private byte[] _bytes = EMPTY;

    /**
     * The stream used for writes
     */
    private DataOutputStream _out = null;

    /**
     * The byte stream backing the output stream
     */
    private ByteArrayOutputStream _byteOut = null;

    /**
     * The stream used for reads
     */
    private DataInputStream _in = null;

    /**
     * The byte stream backing the input stream
     */
    private ByteArrayInputStream _byteIn = null;

    /**
     * Non-zero if incrementally reading a byte array using
     * {@link #readBytes(byte[])}
     */
    private int _readBytes = 0;

    /**
     * The length of the byte array being read using {@link #readBytes(byte[])}
     */
    private int _byteArrayLength = 0;

    /**
     * The offset of the byte stream to start reading from. This defaults
     * to 0, and only applies to messages that are cloned from a
     * read-only instance where part of the stream had already been read.
     */
    private int _offset = 0;


    /**
     * Construct a new StreamMessage. When first created, the message is in
     * write-only mode.
     *
     * @throws JMSException if the message type can't be set, or an I/O error
     * occurs
     */
    public StreamMessageImpl() throws JMSException {
        setJMSType("StreamMessage");
    }

    /**
     * Clone an instance of this object
     *
     * @return a copy of this object
     * @throws CloneNotSupportedException if object or attributes aren't
     * cloneable
     */
    public final Object clone() throws CloneNotSupportedException {
        StreamMessageImpl result = (StreamMessageImpl) super.clone();
        if (_bodyReadOnly) {
            result._bytes = new byte[_bytes.length];
            System.arraycopy(_bytes, 0, result._bytes, 0, _bytes.length);
            if (_byteIn != null) {
                // if a client subsequently reads from the cloned object,
                // start reading from offset of the original stream
                _offset = _bytes.length - _byteIn.available();
            }
            result._byteIn = null;
            result._in = null;
        } else {
            if (_out != null) {
                try {
                    _out.flush();
                } catch (IOException exception) {
                    throw new CloneNotSupportedException(
                        exception.getMessage());
                }
                result._bytes = _byteOut.toByteArray();
                result._byteOut = null;
                result._out = null;
            } else {
                result._bytes = new byte[_bytes.length];
                System.arraycopy(_bytes, 0, result._bytes, 0, _bytes.length);
            }
        }

        return result;
    }

    /**
     * Serialize out this message's data
     *
     * @param out the stream to serialize out to
     * @throws IOException if any I/O exceptions occurr
     */
    public final void writeExternal(ObjectOutput out) throws IOException {
        // If it was in write mode, extract the byte array
        if (!_bodyReadOnly && _out != null) {
            _out.flush();
            _bytes = _byteOut.toByteArray();
        }

        super.writeExternal(out);
        out.writeLong(serialVersionUID);
        out.writeInt(_bytes.length);
        out.write(_bytes);
        out.flush();
    }

    /**
     * Serialize in this message's data
     *
     * @param in the stream to serialize in from
     * @throws ClassNotFoundException if the class for an object being
     * restored cannot be found.
     * @throws IOException if any I/O exceptions occur
     */
    public final void readExternal(ObjectInput in)
        throws ClassNotFoundException, IOException {
        super.readExternal(in);
        long version = in.readLong();
        if (version == serialVersionUID) {
            int length = in.readInt();
            _bytes = new byte[length];
            in.readFully(_bytes);
        } else {
            throw new IOException("Incorrect version enountered: " + version
                                  + ". This version = " + serialVersionUID);
        }
    }

    /**
     * Read a <code>boolean</code> from the bytes message stream
     *
     * @return the <code>boolean</code> value read
     * @throws JMSException if JMS fails to read message due to some internal
     * JMS error
     * @throws MessageEOFException if end of message stream
     * @throws MessageFormatException if this type conversion is invalid
     * @throws MessageNotReadableException if message is in write-only mode
     */
    public final boolean readBoolean() throws JMSException {
        boolean result = false;
        prepare();
        try {
            result = FormatConverter.getBoolean(readNext());
        } catch (MessageFormatException exception) {
            revert(exception);
        }
        return result;
    }

    /**
     * Read a byte value from the stream message
     *
     * @return the next byte from the stream message as an 8-bit
     * <code>byte</code>
     * @throws JMSException if JMS fails to read message due to some internal
     * JMS error
     * @throws MessageEOFException if end of message stream
     * @throws MessageFormatException if this type conversion is invalid
     * @throws MessageNotReadableException if message is in write-only mode
     * @throws NumberFormatException if numeric conversion is invalid
     */
    public final byte readByte() throws JMSException {
        byte result = 0;
        prepare();
        try {
            result = FormatConverter.getByte(readNext());
        } catch (MessageFormatException exception) {
            revert(exception);
        } catch (NumberFormatException exception) {
            revert(exception);
        }
        return result;
    }

    /**
     * Read a 16-bit number from the stream message.
     *
     * @return a 16-bit number from the stream message
     * @throws JMSException if JMS fails to read message due to some internal
     * JMS error
     * @throws MessageEOFException if end of message stream
     * @throws MessageFormatException if this type conversion is invalid
     * @throws MessageNotReadableException if message is in write-only mode
     * @throws NumberFormatException if numeric conversion is invalid
     */
    public final short readShort() throws JMSException {
        short result = 0;
        prepare();
        try {
            result = FormatConverter.getShort(readNext());
        } catch (MessageFormatException exception) {
            revert(exception);
        } catch (NumberFormatException exception) {
            revert(exception);
        }
        return result;
    }

    /*
     * Read a Unicode character value from the stream message
     *
     * @return a Unicode character from the stream message
     * @throws JMSException if JMS fails to read message due to some internal
     * JMS error
     * @throws MessageEOFException if end of message stream
     * @throws MessageFormatException if this type conversion is invalid
     * @throws MessageNotReadableException if message is in write-only mode
     */
    public final char readChar() throws JMSException {
        char result = 0;
        prepare();
        try {
            result = FormatConverter.getChar(readNext());
        } catch (MessageFormatException exception) {
            revert(exception);
        } catch (NullPointerException exception) {
            revert(exception);
        }
        return result;
    }

⌨️ 快捷键说明

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