hessian2input.java

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

JAVA
2,784
字号
    throws IOException  {    String type = readType();    String url = readString();    return resolveRemote(type, url);  }  /**   * Reads a reference.   */  public Object readRef()    throws IOException  {    return _refs.get(parseInt());  }  /**   * Reads the start of a list.   */  public int readListStart()    throws IOException  {    return read();  }  /**   * Reads the start of a list.   */  public int readMapStart()    throws IOException  {    return read();  }  /**   * Returns true if this is the end of a list or a map.   */  public boolean isEnd()    throws IOException  {    int code;    if (_offset < _length)      code = (_buffer[_offset] & 0xff);    else {      code = read();      if (code >= 0)	_offset--;    }    return (code < 0 || code == 'z');  }  /**   * Reads the end byte.   */  public void readEnd()    throws IOException  {    int code = _offset < _length ? (_buffer[_offset++] & 0xff) : read();    if (code == 'z')      return;    else if (code < 0)      throw error("unexpected end of file");    else      throw error("unknown code:" + codeName(code));  }  /**   * Reads the end byte.   */  public void readMapEnd()    throws IOException  {    int code = _offset < _length ? (_buffer[_offset++] & 0xff) : read();    if (code != 'z')      throw error("expected end of map ('z') at '" + codeName(code) + "'");  }  /**   * Reads the end byte.   */  public void readListEnd()    throws IOException  {    int code = _offset < _length ? (_buffer[_offset++] & 0xff) : read();    if (code != 'z')      throw error("expected end of list ('z') at '" + codeName(code) + "'");  }  /**   * Adds a list/map reference.   */  public int addRef(Object ref)  {    if (_refs == null)      _refs = new ArrayList();        _refs.add(ref);    return _refs.size() - 1;  }  /**   * Adds a list/map reference.   */  public void setRef(int i, Object ref)  {    _refs.set(i, ref);  }    /**   * Resets the references for streaming.   */  public void resetReferences()  {    if (_refs != null)      _refs.clear();  }  public Object readStreamingObject()    throws IOException  {    if (_refs != null)      _refs.clear();    return readObject();  }  /**   * Resolves a remote object.   */  public Object resolveRemote(String type, String url)    throws IOException  {    HessianRemoteResolver resolver = getRemoteResolver();    if (resolver != null)      return resolver.lookup(type, url);    else      return new HessianRemote(type, url);  }  /**   * Parses a type from the stream.   *   * <pre>   * t b16 b8   * </pre>   */  public String readType()    throws IOException  {    int code = _offset < _length ? (_buffer[_offset++] & 0xff) : read();    switch (code) {    case 't':      {        int len = 256 * read() + read();        String type = readLenString(len);        if (_types == null)          _types = new ArrayList();        _types.add(type);        return type;      }    case 'T':      {        int ref = readInt();        return (String) _types.get(ref);      }    case TYPE_REF:      {        int ref = readInt();        return (String) _types.get(ref);      }          default:      {        if (code >= 0)          _offset--;              return "";      }    }  }  /**   * Parses the length for an array   *   * <pre>   * l b32 b24 b16 b8   * </pre>   */  public int readLength()    throws IOException  {    int code = read();    if (code == LENGTH_BYTE)      return read();	    else if (code == 'l')      return parseInt();    else {      if (code >= 0)	_offset--;            return -1;    }  }  /**   * Parses a 32-bit integer value from the stream.   *   * <pre>   * b32 b24 b16 b8   * </pre>   */  private int parseInt()    throws IOException  {    int offset = _offset;        if (offset + 3 < _length) {      byte []buffer = _buffer;            int b32 = buffer[offset + 0] & 0xff;      int b24 = buffer[offset + 1] & 0xff;      int b16 = buffer[offset + 2] & 0xff;      int b8 = buffer[offset + 3] & 0xff;      _offset = offset + 4;      return (b32 << 24) + (b24 << 16) + (b16 << 8) + b8;    }    else {      int b32 = read();      int b24 = read();      int b16 = read();      int b8 = read();      return (b32 << 24) + (b24 << 16) + (b16 << 8) + b8;    }  }  /**   * Parses a 64-bit long value from the stream.   *   * <pre>   * b64 b56 b48 b40 b32 b24 b16 b8   * </pre>   */  private long parseLong()    throws IOException  {    long b64 = read();    long b56 = read();    long b48 = read();    long b40 = read();    long b32 = read();    long b24 = read();    long b16 = read();    long b8 = read();    return ((b64 << 56) +            (b56 << 48) +            (b48 << 40) +            (b40 << 32) +            (b32 << 24) +            (b24 << 16) +            (b16 << 8) +            b8);  }    /**   * Parses a 64-bit double value from the stream.   *   * <pre>   * b64 b56 b48 b40 b32 b24 b16 b8   * </pre>   */  private double parseDouble()    throws IOException  {    long bits = parseLong();      return Double.longBitsToDouble(bits);  }  org.w3c.dom.Node parseXML()    throws IOException  {    throw new UnsupportedOperationException();  }    /**   * Reads a character from the underlying stream.   */  private int parseChar()    throws IOException  {    while (_chunkLength <= 0) {      if (_isLastChunk)        return -1;      int code = _offset < _length ? (_buffer[_offset++] & 0xff) : read();      switch (code) {      case 's':      case 'x':        _isLastChunk = false;        _chunkLength = (read() << 8) + read();        break;              case 'S':      case 'X':        _isLastChunk = true;        _chunkLength = (read() << 8) + read();        break;	      case 0x00: case 0x01: case 0x02: case 0x03:      case 0x04: case 0x05: case 0x06: case 0x07:      case 0x08: case 0x09: case 0x0a: case 0x0b:      case 0x0c: case 0x0d: case 0x0e: case 0x0f:      case 0x10: case 0x11: case 0x12: case 0x13:      case 0x14: case 0x15: case 0x16: case 0x17:      case 0x18: case 0x19: case 0x1a: case 0x1b:      case 0x1c: case 0x1d: case 0x1e: case 0x1f:	_isLastChunk = true;	_chunkLength = code - 0x00;	break;      default:        throw expect("string", code);      }    }    _chunkLength--;    return parseUTF8Char();  }  /**   * Parses a single UTF8 character.   */  private int parseUTF8Char()    throws IOException  {    int ch = _offset < _length ? (_buffer[_offset++] & 0xff) : read();    if (ch < 0x80)      return ch;    else if ((ch & 0xe0) == 0xc0) {      int ch1 = read();      int v = ((ch & 0x1f) << 6) + (ch1 & 0x3f);      return v;    }    else if ((ch & 0xf0) == 0xe0) {      int ch1 = read();      int ch2 = read();      int v = ((ch & 0x0f) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f);      return v;    }    else      throw error("bad utf-8 encoding at " + codeName(ch));  }    /**   * Reads a byte from the underlying stream.   */  private int parseByte()    throws IOException  {    while (_chunkLength <= 0) {      if (_isLastChunk) {        return -1;      }      int code = read();      switch (code) {      case 'b':        _isLastChunk = false;        _chunkLength = (read() << 8) + read();        break;              case 'B':        _isLastChunk = true;        _chunkLength = (read() << 8) + read();        break;      case 0x20: case 0x21: case 0x22: case 0x23:      case 0x24: case 0x25: case 0x26: case 0x27:      case 0x28: case 0x29: case 0x2a: case 0x2b:      case 0x2c: case 0x2d: case 0x2e: case 0x2f:        _isLastChunk = true;        _chunkLength = code - 0x20;        break;      default:        throw expect("byte[]", code);      }    }    _chunkLength--;    return read();  }  /**   * Reads bytes based on an input stream.   */  public InputStream readInputStream()    throws IOException  {    int tag = read();    switch (tag) {    case 'N':      return null;    case 'B':    case 'b':      _isLastChunk = tag == 'B';      _chunkLength = (read() << 8) + read();      break;    case 0x20: case 0x21: case 0x22: case 0x23:    case 0x24: case 0x25: case 0x26: case 0x27:    case 0x28: case 0x29: case 0x2a: case 0x2b:    case 0x2c: case 0x2d: case 0x2e: case 0x2f:      _isLastChunk = true;      _chunkLength = tag - 0x20;      break;          default:      throw expect("binary", tag);    }        return new ReadInputStream();  }    /**   * Reads bytes from the underlying stream.   */  int read(byte []buffer, int offset, int length)    throws IOException  {    int readLength = 0;    while (length > 0) {      while (_chunkLength <= 0) {        if (_isLastChunk)          return readLength == 0 ? -1 : readLength;        int code = read();        switch (code) {        case 'b':          _isLastChunk = false;          _chunkLength = (read() << 8) + read();          break;                case 'B':          _isLastChunk = true;          _chunkLength = (read() << 8) + read();          break;	case 0x20: case 0x21: case 0x22: case 0x23:	case 0x24: case 0x25: case 0x26: case 0x27:	case 0x28: case 0x29: case 0x2a: case 0x2b:	case 0x2c: case 0x2d: case 0x2e: case 0x2f:	  _isLastChunk = true;	  _chunkLength = code - 0x20;	  break;        default:          throw expect("byte[]", code);        }      }      int sublen = _chunkLength;      if (length < sublen)        sublen = length;      if (_length <= _offset && ! readBuffer())	return -1;            if (_length - _offset < sublen)	sublen = _length - _offset;      System.arraycopy(_buffer, _offset, buffer, offset, sublen);      _offset += sublen;            offset += sublen;      readLength += sublen;      length -= sublen;      _chunkLength -= sublen;    }    return readLength;  }  /**   * Normally, shouldn't be called externally, but needed for QA, e.g.   * ejb/3b01.   */  public final int read()    throws IOException  {    if (_length <= _offset && ! readBuffer())      return -1;    return _buffer[_offset++] & 0xff;  }  private final boolean readBuffer()    throws IOException  {    byte []buffer = _buffer;    int offset = _offset;    int length = _length;        if (offset < length) {      System.arraycopy(buffer, offset, buffer, 0, length - offset);      offset = length - offset;    }    else      offset = 0;        int len = _is.read(buffer, offset, SIZE - offset);    if (len <= 0) {      _length = offset;      _offset = 0;            return offset > 0;    }    _length = offset + len;    _offset = 0;    return true;  }  public Reader getReader()  {    return null;  }  protected IOException expect(String expect, int ch)    throws IOException  {    if (ch < 0)      return error("expected " + expect + " at end of file");    else {      _offset--;      try {	Object obj = readObject();	if (obj != null) {	  return error("expected " + expect		       + " at 0x" + Integer.toHexString(ch & 0xff)		       + " " + obj.getClass().getName() + " (" + obj + ")");	}	else	  return error("expected " + expect		       + " at 0x" + Integer.toHexString(ch & 0xff) + " null");      } catch (IOException e) {	log.log(Level.FINE, e.toString(), e);		return error("expected " + expect		     + " at 0x" + Integer.toHexString(ch & 0xff));      }    }  }  protected String codeName(int ch)  {    if (ch < 0)      return "end of file";    else      return "0x" + Integer.toHexString(ch & 0xff) + " (" + (char) + ch + ")";  }    protected IOException error(String message)  {    if (_method != null)      return new HessianProtocolException(_method + ": " + message);    else      return new HessianProtocolException(message);  }  public void close()    throws IOException  {    InputStream is = _is;    _is = null;    if (_isCloseStreamOnClose && is != null)      is.close();  }    class ReadInputStream extends InputStream {    boolean _isClosed = false;	    public int read()      throws IOException    {      if (_isClosed)	return -1;      int ch = parseByte();      if (ch < 0)	_isClosed = true;      return ch;    }	    public int read(byte []buffer, int offset, int length)      throws IOException    {      if (_isClosed)	return -1;      int len = Hessian2Input.this.read(buffer, offset, length);      if (len < 0)	_isClosed = true;      return len;    }    public void close()      throws IOException    {      while (read() >= 0) {      }    }  };  final static class ObjectDefinition {    private final String _type;    private final String []_fields;    ObjectDefinition(String type, String []fields)    {      _type = type;      _fields = fields;    }    String getType()    {      return _type;    }    String []getFieldNames()    {      return _fields;    }  }  static {    try {      _detailMessageField = Throwable.class.getDeclaredField("detailMessage");      _detailMessageField.setAccessible(true);    } catch (Throwable e) {    }  }}

⌨️ 快捷键说明

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