bytesmessageimpl.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 765 行 · 第 1/2 页

JAVA
765
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.jms.message;import com.caucho.jms.JMSExceptionWrapper;import com.caucho.util.CharBuffer;import com.caucho.vfs.*;import javax.jms.BytesMessage;import javax.jms.JMSException;import javax.jms.MessageEOFException;import javax.jms.MessageFormatException;import java.io.*;import java.util.logging.Level;/** * A byte-stream message. */public class BytesMessageImpl extends MessageImpl implements BytesMessage {  private TempStream _tempStream;  private ReadStream _rs;  private WriteStream _ws;  public BytesMessageImpl()  {  }  BytesMessageImpl(BytesMessage bytes)    throws JMSException  {    super(bytes);    bytes.reset();    checkBodyWriteable();    try {      TempBuffer tempBuf = TempBuffer.allocate();      byte []buffer = tempBuf.getBuffer();      WriteStream out = getWriteStream();      int sublen;          while ((sublen = bytes.readBytes(buffer, buffer.length)) > 0) {        out.write(buffer, 0, sublen);      }      TempBuffer.free(tempBuf);      tempBuf = null;    } catch (IOException e) {      log.log(Level.FINE, e.toString(), e);            throw new JMSException(e.toString());    }    reset();  }  BytesMessageImpl(BytesMessageImpl bytes)    throws JMSException  {    super(bytes);    bytes.reset();    _tempStream = bytes._tempStream;    reset();  }  /**   * Returns the type enumeration.   */  @Override  public MessageType getType()  {    return MessageType.BYTES;  }  /**   * Sets the body for reading.   */  public void setReceive()    throws JMSException  {    super.setReceive();        reset();  }  /**   * Set the stream for reading.   */  public void reset()    throws JMSException  {    setBodyReadOnly();        try {      // XXX: test for null      if (_ws != null)	_ws.close();      if (_tempStream != null) {	if (_rs != null)	  _rs.close();		_rs = _tempStream.openReadAndSaveBuffer();      }    } catch (IOException e) {      throw new JMSExceptionWrapper(e);    }  }  /**   * Read a boolean from the stream.   */  public boolean readBoolean()    throws JMSException  {    ReadStream is = getReadStream();    try {      int value = is.read();      if (value >= 0)        return value == 1;      else        throw new MessageEOFException("BytesMessage EOF");    } catch (IOException e) {      throw new JMSExceptionWrapper(e);    }  }  /**   * Read a byte from the stream.   */  public byte readByte()    throws JMSException  {    ReadStream is = getReadStream();    try {      int value = is.read();      if (value >= 0)        return (byte) value;      else        throw new MessageEOFException("BytesMessage EOF");    } catch (IOException e) {      throw new JMSExceptionWrapper(e);    }  }  /**   * Read an unsigned byte from the stream.   */  public int readUnsignedByte()    throws JMSException  {    ReadStream is = getReadStream();    if (is == null)      return -1;    try {      int value = is.read();      if (value >= 0)        return value;      else        throw new MessageEOFException("BytesMessage EOF");    } catch (IOException e) {      throw new JMSExceptionWrapper(e);    }  }  /**   * Read a short from the stream.   */  public short readShort()    throws JMSException  {    ReadStream is = getReadStream();    try {      int d1 = is.read();          int d2 = is.read();          if (d2 >= 0)        return (short) ((d1 << 8) + d2);      else        throw new MessageEOFException("BytesMessage EOF");    } catch (IOException e) {      throw new JMSExceptionWrapper(e);    }  }  /**   * Read an unsigned short from the stream.   */  public int readUnsignedShort()    throws JMSException  {    ReadStream is = getReadStream();    try {      int d1 = is.read();          int d2 = is.read();          if (d2 >= 0)        return ((d1 << 8) + d2);      else        throw new MessageEOFException("BytesMessage EOF");    } catch (IOException e) {      throw new JMSExceptionWrapper(e);    }  }  /**   * Read an integer from the stream.   */  public int readInt()    throws JMSException  {    ReadStream is = getReadStream();    try {      int d1 = is.read();          int d2 = is.read();          int d3 = is.read();          int d4 = is.read();          if (d4 >= 0)        return (d1 << 24) + (d2 << 16) + (d3 << 8) + d4;      else        throw new MessageEOFException("BytesMessage EOF");    } catch (IOException e) {      throw new JMSExceptionWrapper(e);    }  }  /**   * Read a long from the stream.   */  public long readLong()    throws JMSException  {    ReadStream is = getReadStream();    try {      long d1 = is.read();          long d2 = is.read();          long d3 = is.read();          long d4 = is.read();          long d5 = is.read();          long d6 = is.read();          long d7 = is.read();          long d8 = is.read();          if (d8 >= 0) {        return ((d1 << 56)                + (d2 << 48)                + (d3 << 40)                + (d4 << 32)                + (d5 << 24)                + (d6 << 16)                + (d7 << 8)                + (d8));      }      else        throw new MessageEOFException("BytesMessage EOF");    } catch (IOException e) {      throw new JMSExceptionWrapper(e);    }  }  /**   * Read a float from the stream.   */  public float readFloat()    throws JMSException  {    return Float.intBitsToFloat(readInt());  }  /**   * Read a double from the stream.   */  public double readDouble()    throws JMSException  {    return Double.longBitsToDouble(readLong());  }  /**   * Read a character object from the stream.   */  public char readChar()    throws JMSException  {    ReadStream is = getReadStream();    try {      int d1 = is.read();          int d2 = is.read();      if (d2 >= 0)        return (char) ((d1 << 8) + d2);      else        throw new MessageEOFException("BytesMessage EOF");    } catch (IOException e) {      throw new JMSExceptionWrapper(e);    }  }  /**   * Read a string from the stream.   */  public String readUTF()    throws JMSException  {    ReadStream is = getReadStream();    CharBuffer cb = new CharBuffer();    try {      int len = readShort();      int d1;            while (len > 0) {        d1 = is.read();                if (d1 < 0x80) {          cb.append((char) d1);          len -= 1;        }        else if ((d1 & 0xe0) == 0xc0) {          int d2 = is.read();          cb.append((char) (((d1 & 0x1f) << 6) + (d2 & 0x3f)));          len -= 2;        }        else if ((d1 & 0xf0) == 0xe0) {          int d2 = is.read();          int d3 = is.read();          cb.append((char) (((d1 & 0xf) << 12)                            + ((d2 & 0x3f) << 6)                            + (d3 & 0x3f)));          len -= 3;        }	else	  throw new MessageFormatException(L.l("invalid UTF-8 in bytes message"));

⌨️ 快捷键说明

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