hessian2output.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,662 行 · 第 1/3 页

JAVA
1,662
字号
      if (SIZE < _offset + 16)	flush();	      if (length <= STRING_DIRECT_MAX) {	_buffer[_offset++] = (byte) (STRING_DIRECT + length);      }      else {	_buffer[_offset++] = (byte) ('S');	_buffer[_offset++] = (byte) (length >> 8);	_buffer[_offset++] = (byte) (length);      }      printString(buffer, offset, length);    }  }  /**   * Writes a byte array to the stream.   * The array will be written with the following syntax:   *   * <code><pre>   * B b16 b18 bytes   * </pre></code>   *   * If the value is null, it will be written as   *   * <code><pre>   * N   * </pre></code>   *   * @param value the string value to write.   */  public void writeBytes(byte []buffer)    throws IOException  {    if (buffer == null) {      if (SIZE < _offset + 16)	flush();      _buffer[_offset++] = 'N';    }    else      writeBytes(buffer, 0, buffer.length);  }    /**   * Writes a byte array to the stream.   * The array will be written with the following syntax:   *   * <code><pre>   * B b16 b18 bytes   * </pre></code>   *   * If the value is null, it will be written as   *   * <code><pre>   * N   * </pre></code>   *   * @param value the string value to write.   */  public void writeBytes(byte []buffer, int offset, int length)    throws IOException  {    if (buffer == null) {      if (SIZE < _offset + 16)	flushBuffer();            _buffer[_offset++] = (byte) 'N';    }    else {      flush();      while (length > SIZE - _offset - 3) {        int sublen = SIZE - _offset - 3;        if (sublen < 16) {          flushBuffer();          sublen = SIZE - _offset - 3;          if (length < sublen)            sublen = length;        }        _buffer[_offset++] = (byte) 'b';        _buffer[_offset++] = (byte) (sublen >> 8);        _buffer[_offset++] = (byte) sublen;        System.arraycopy(buffer, offset, _buffer, _offset, sublen);        _offset += sublen;        length -= sublen;        offset += sublen;      }      if (SIZE < _offset + 16)        flushBuffer();      if (length < 0x10) {        _buffer[_offset++] = (byte) (BYTES_DIRECT + length);      }      else {        _buffer[_offset++] = (byte) 'B';        _buffer[_offset++] = (byte) (length >> 8);        _buffer[_offset++] = (byte) (length);      }      System.arraycopy(buffer, offset, _buffer, _offset, length);      _offset += length;    }  }    /**   * Writes a byte buffer to the stream.   *   * <code><pre>   * </pre></code>   */  public void writeByteBufferStart()    throws IOException  {  }    /**   * Writes a byte buffer to the stream.   *   * <code><pre>   * b b16 b18 bytes   * </pre></code>   */  public void writeByteBufferPart(byte []buffer, int offset, int length)    throws IOException  {    while (length > 0) {      int sublen = length;      if (0x8000 < sublen)	sublen = 0x8000;      flush(); // bypass buffer            _os.write('b');      _os.write(sublen >> 8);      _os.write(sublen);      _os.write(buffer, offset, sublen);      length -= sublen;      offset += sublen;    }  }    /**   * Writes a byte buffer to the stream.   *   * <code><pre>   * b b16 b18 bytes   * </pre></code>   */  public void writeByteBufferEnd(byte []buffer, int offset, int length)    throws IOException  {    writeBytes(buffer, offset, length);  }  /**   * Returns an output stream to write binary data.   */  public OutputStream getBytesOutputStream()    throws IOException  {    return new BytesOutputStream();  }  /**   * Writes a reference.   *   * <code><pre>   * R b32 b24 b16 b8   * </pre></code>   *   * @param value the integer value to write.   */  public void writeRef(int value)    throws IOException  {    if (SIZE < _offset + 16)      flush();        if (value < 0x100) {      _buffer[_offset++] = (byte) (REF_BYTE);      _buffer[_offset++] = (byte) (value);    }    else if (value < 0x10000) {      _buffer[_offset++] = (byte) (REF_SHORT);      _buffer[_offset++] = (byte) (value >> 8);      _buffer[_offset++] = (byte) (value);    }    else {      _buffer[_offset++] = (byte) ('R');      _buffer[_offset++] = (byte) (value >> 24);      _buffer[_offset++] = (byte) (value >> 16);      _buffer[_offset++] = (byte) (value >> 8);      _buffer[_offset++] = (byte) (value);    }  }  /**   * If the object has already been written, just write its ref.   *   * @return true if we're writing a ref.   */  public boolean addRef(Object object)    throws IOException  {    int ref = _refs.get(object);    if (ref >= 0) {      writeRef(ref);            return true;    }    else {      _refs.put(object, _refs.size());            return false;    }  }  /**   * Removes a reference.   */  public boolean removeRef(Object obj)    throws IOException  {    if (_refs != null) {      _refs.remove(obj);      return true;    }    else      return false;  }  /**   * Replaces a reference from one object to another.   */  public boolean replaceRef(Object oldRef, Object newRef)    throws IOException  {    Integer value = (Integer) _refs.remove(oldRef);    if (value != null) {      _refs.put(newRef, value);      return true;    }    else      return false;  }  /**   * Resets the references for streaming.   */  public void resetReferences()  {    if (_refs != null)      _refs.clear();  }  /**   * Starts the streaming message   *   * <p>A streaming message starts with 'P'</p>   *   * <pre>   * P x02 x00   * </pre>   */  public void writeStreamingObject(Object obj)    throws IOException  {    if (_refs != null)      _refs.clear();        flush();    _isStreaming = true;    _offset = 3;    writeObject(obj);    int len = _offset - 3;        _buffer[0] = (byte) 'P';    _buffer[1] = (byte) (len >> 8);    _buffer[2] = (byte) len;    _isStreaming = false;    flush();  }  /**   * Prints a string to the stream, encoded as UTF-8 with preceeding length   *   * @param v the string to print.   */  public void printLenString(String v)    throws IOException  {    if (SIZE < _offset + 16)      flush();        if (v == null) {      _buffer[_offset++] = (byte) (0);      _buffer[_offset++] = (byte) (0);    }    else {      int len = v.length();      _buffer[_offset++] = (byte) (len >> 8);      _buffer[_offset++] = (byte) (len);      printString(v, 0, len);    }  }  /**   * Prints a string to the stream, encoded as UTF-8   *   * @param v the string to print.   */  public void printString(String v)    throws IOException  {    printString(v, 0, v.length());  }    /**   * Prints a string to the stream, encoded as UTF-8   *   * @param v the string to print.   */  public void printString(String v, int strOffset, int length)    throws IOException  {    int offset = _offset;    byte []buffer = _buffer;        for (int i = 0; i < length; i++) {      if (SIZE <= offset + 16) {	_offset = offset;	flush();	offset = _offset;      }            char ch = v.charAt(i + strOffset);      if (ch < 0x80)        buffer[offset++] = (byte) (ch);      else if (ch < 0x800) {        buffer[offset++] = (byte) (0xc0 + ((ch >> 6) & 0x1f));        buffer[offset++] = (byte) (0x80 + (ch & 0x3f));      }      else {        buffer[offset++] = (byte) (0xe0 + ((ch >> 12) & 0xf));        buffer[offset++] = (byte) (0x80 + ((ch >> 6) & 0x3f));        buffer[offset++] = (byte) (0x80 + (ch & 0x3f));      }    }    _offset = offset;  }    /**   * Prints a string to the stream, encoded as UTF-8   *   * @param v the string to print.   */  public void printString(char []v, int strOffset, int length)    throws IOException  {    int offset = _offset;    byte []buffer = _buffer;        for (int i = 0; i < length; i++) {      if (SIZE <= offset + 16) {	_offset = offset;	flush();	offset = _offset;      }            char ch = v[i + strOffset];      if (ch < 0x80)        buffer[offset++] = (byte) (ch);      else if (ch < 0x800) {        buffer[offset++] = (byte) (0xc0 + ((ch >> 6) & 0x1f));        buffer[offset++] = (byte) (0x80 + (ch & 0x3f));      }      else {        buffer[offset++] = (byte) (0xe0 + ((ch >> 12) & 0xf));        buffer[offset++] = (byte) (0x80 + ((ch >> 6) & 0x3f));        buffer[offset++] = (byte) (0x80 + (ch & 0x3f));      }    }    _offset = offset;  }    private final void flushIfFull()    throws IOException  {    int offset = _offset;        if (SIZE < offset + 32) {      _offset = 0;      _os.write(_buffer, 0, offset);    }  }  public final void flush()    throws IOException  {    flushBuffer();    _os.flush();  }  public final void flushBuffer()    throws IOException  {    int offset = _offset;    if (! _isStreaming && offset > 0) {      _offset = 0;            _os.write(_buffer, 0, offset);    }    else if (_isStreaming && offset > 3) {      int len = offset - 3;      _buffer[0] = 'p';      _buffer[1] = (byte) (len >> 8);      _buffer[2] = (byte) len;      _offset = 3;      _os.write(_buffer, 0, offset);    }  }  public final void close()    throws IOException  {    flushBuffer();        OutputStream os = _os;    _os = null;    if (os != null) {      if (_isCloseStreamOnClose)	os.close();    }  }  class BytesOutputStream extends OutputStream {    private int _startOffset;        BytesOutputStream()      throws IOException    {      if (SIZE < _offset + 16) {        Hessian2Output.this.flush();      }      _startOffset = _offset;      _offset += 3; // skip 'b' xNN xNN    }    @Override    public void write(int ch)      throws IOException    {      if (SIZE <= _offset) {        int length = (_offset - _startOffset) - 3;        _buffer[_startOffset] = (byte) 'b';        _buffer[_startOffset + 1] = (byte) (length >> 8);        _buffer[_startOffset + 2] = (byte) (length);        Hessian2Output.this.flush();        _startOffset = _offset;        _offset += 3;      }      _buffer[_offset++] = (byte) ch;    }    @Override    public void write(byte []buffer, int offset, int length)      throws IOException    {      while (length > 0) {        int sublen = SIZE - _offset;        if (length < sublen)          sublen = length;        if (sublen > 0) {          System.arraycopy(buffer, offset, _buffer, _offset, sublen);          _offset += sublen;        }        length -= sublen;        offset += sublen;        if (SIZE <= _offset) {          int chunkLength = (_offset - _startOffset) - 3;          _buffer[_startOffset] = (byte) 'b';          _buffer[_startOffset + 1] = (byte) (chunkLength >> 8);          _buffer[_startOffset + 2] = (byte) (chunkLength);          Hessian2Output.this.flush();          _startOffset = _offset;          _offset += 3;        }      }    }    @Override    public void close()      throws IOException    {      int startOffset = _startOffset;      _startOffset = -1;      if (startOffset < 0)        return;      int length = (_offset - startOffset) - 3;      _buffer[startOffset] = (byte) 'B';      _buffer[startOffset + 1] = (byte) (length >> 8);      _buffer[startOffset + 2] = (byte) (length);      Hessian2Output.this.flush();    }  }}

⌨️ 快捷键说明

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