hessian2input.java

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

JAVA
2,784
字号
      _chunkLength = tag - 0x00;      _sbuf.setLength(0);      while ((ch = parseChar()) >= 0)        _sbuf.append((char) ch);      return _sbuf.toString();    default:      throw expect("string", tag);    }  }  /**   * Reads an XML node.   *   * <pre>   * S b16 b8 string value   * </pre>   */  public org.w3c.dom.Node readNode()    throws IOException  {    int tag = read();    switch (tag) {    case 'N':      return null;    case 'S':    case 's':    case 'X':    case 'x':      _isLastChunk = tag == 'S' || tag == 'X';      _chunkLength = (read() << 8) + read();      throw error("XML is not supported");    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 = tag - 0x00;      throw error("XML is not supported");    default:      throw expect("string", tag);    }  }  /**   * Reads a byte array   *   * <pre>   * B b16 b8 data value   * </pre>   */  public byte []readBytes()    throws IOException  {    int tag = read();    switch (tag) {    case 'N':      return null;    case 'B':    case 'b':      _isLastChunk = tag == 'B';      _chunkLength = (read() << 8) + read();      ByteArrayOutputStream bos = new ByteArrayOutputStream();      int data;      while ((data = parseByte()) >= 0)        bos.write(data);      return bos.toByteArray();    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;      bos = new ByteArrayOutputStream();      while ((data = parseByte()) >= 0)        bos.write(data);      return bos.toByteArray();          default:      throw expect("bytes", tag);    }  }  /**   * Reads a byte from the stream.   */  public int readByte()    throws IOException  {    if (_chunkLength > 0) {      _chunkLength--;      if (_chunkLength == 0 && _isLastChunk)        _chunkLength = END_OF_DATA;      return read();    }    else if (_chunkLength == END_OF_DATA) {      _chunkLength = 0;      return -1;    }        int tag = read();    switch (tag) {    case 'N':      return -1;    case 'B':    case 'b':      _isLastChunk = tag == 'B';      _chunkLength = (read() << 8) + read();      int value = parseByte();      // special code so successive read byte won't      // be read as a single object.      if (_chunkLength == 0 && _isLastChunk)        _chunkLength = END_OF_DATA;      return value;          default:      throw expect("binary", tag);    }  }  /**   * Reads a byte array from the stream.   */  public int readBytes(byte []buffer, int offset, int length)    throws IOException  {    int readLength = 0;    if (_chunkLength == END_OF_DATA) {      _chunkLength = 0;      return -1;    }    else if (_chunkLength == 0) {      int tag = read();      switch (tag) {      case 'N':        return -1;            case 'B':      case 'b':        _isLastChunk = tag == 'B';        _chunkLength = (read() << 8) + read();        break;            default:        throw expect("binary", tag);      }    }    while (length > 0) {      if (_chunkLength > 0) {        buffer[offset++] = (byte) read();        _chunkLength--;        length--;        readLength++;      }      else if (_isLastChunk) {        if (readLength == 0)          return -1;        else {          _chunkLength = END_OF_DATA;          return readLength;        }      }      else {        int tag = read();        switch (tag) {        case 'B':        case 'b':          _isLastChunk = tag == 'B';          _chunkLength = (read() << 8) + read();          break;              default:          throw expect("binary", tag);        }      }    }        if (readLength == 0)      return -1;    else if (_chunkLength > 0 || ! _isLastChunk)      return readLength;    else {      _chunkLength = END_OF_DATA;      return readLength;    }  }  /**   * Reads a fault.   */  private HashMap readFault()    throws IOException  {    HashMap map = new HashMap();    int code = read();    for (; code > 0 && code != 'z'; code = read()) {      _offset--;            Object key = readObject();      Object value = readObject();      if (key != null && value != null)        map.put(key, value);    }    if (code != 'z')      throw expect("fault", code);    return map;  }  /**   * Reads an object from the input stream with an expected type.   */  public Object readObject(Class cl)    throws IOException  {    if (cl == null || cl == Object.class)      return readObject();        int tag = _offset < _length ? (_buffer[_offset++] & 0xff) : read();    switch (tag) {    case 'N':      return null;    case 'M':    {      String type = readType();      // hessian/3bb3      if ("".equals(type)) {	Deserializer reader;	reader = findSerializerFactory().getDeserializer(cl);	return reader.readMap(this);      }      else {	Deserializer reader;	reader = findSerializerFactory().getObjectDeserializer(type, cl);	return reader.readMap(this);      }    }    case 'O':    {      readObjectDefinition(cl);      return readObject(cl);    }    case 'o':    {      int ref = readInt();      int size = _classDefs.size();      if (ref < 0 || size <= ref)	throw new HessianProtocolException("'" + ref + "' is an unknown class definition");      ObjectDefinition def = (ObjectDefinition) _classDefs.get(ref);      return readObjectInstance(cl, def);    }    case 'V':    {      String type = readType();      int length = readLength();            Deserializer reader;      reader = findSerializerFactory().getListDeserializer(type, cl);      Object v = reader.readList(this, length);      return v;    }    case 'v':    {      int ref = readInt();      String type = (String) _types.get(ref);      int length = readInt();            Deserializer reader;      reader = findSerializerFactory().getListDeserializer(type, cl);      Object v = reader.readLengthList(this, length);      return v;    }    case 'R':    {      int ref = parseInt();      return _refs.get(ref);    }    case 'r':    {      String type = readType();      String url = readString();      return resolveRemote(type, url);    }    case REF_BYTE: {      int ref = read();      return _refs.get(ref);    }    case REF_SHORT: {      int ref = 256 * read() + read();      return _refs.get(ref);    }    }    if (tag >= 0)      _offset--;    // hessian/3b2i vs hessian/3406    // return readObject();    Object value = findSerializerFactory().getDeserializer(cl).readObject(this);    return value;  }    /**   * Reads an arbitrary object from the input stream when the type   * is unknown.   */  public Object readObject()    throws IOException  {    int tag = _offset < _length ? (_buffer[_offset++] & 0xff) : read();    switch (tag) {    case 'N':      return null;          case 'T':      return Boolean.valueOf(true);          case 'F':      return Boolean.valueOf(false);      // direct integer    case 0x80: case 0x81: case 0x82: case 0x83:    case 0x84: case 0x85: case 0x86: case 0x87:    case 0x88: case 0x89: case 0x8a: case 0x8b:    case 0x8c: case 0x8d: case 0x8e: case 0x8f:          case 0x90: case 0x91: case 0x92: case 0x93:    case 0x94: case 0x95: case 0x96: case 0x97:    case 0x98: case 0x99: case 0x9a: case 0x9b:    case 0x9c: case 0x9d: case 0x9e: case 0x9f:          case 0xa0: case 0xa1: case 0xa2: case 0xa3:    case 0xa4: case 0xa5: case 0xa6: case 0xa7:    case 0xa8: case 0xa9: case 0xaa: case 0xab:    case 0xac: case 0xad: case 0xae: case 0xaf:          case 0xb0: case 0xb1: case 0xb2: case 0xb3:    case 0xb4: case 0xb5: case 0xb6: case 0xb7:    case 0xb8: case 0xb9: case 0xba: case 0xbb:    case 0xbc: case 0xbd: case 0xbe: case 0xbf:      return Integer.valueOf(tag - INT_ZERO);      /* byte int */    case 0xc0: case 0xc1: case 0xc2: case 0xc3:    case 0xc4: case 0xc5: case 0xc6: case 0xc7:    case 0xc8: case 0xc9: case 0xca: case 0xcb:    case 0xcc: case 0xcd: case 0xce: case 0xcf:      return Integer.valueOf(((tag - INT_BYTE_ZERO) << 8) + read());            /* short int */    case 0xd0: case 0xd1: case 0xd2: case 0xd3:    case 0xd4: case 0xd5: case 0xd6: case 0xd7:      return Integer.valueOf(((tag - INT_SHORT_ZERO) << 16) + 256 * read() + read());          case 'I':      return Integer.valueOf(parseInt());      // direct long    case 0xd8: case 0xd9: case 0xda: case 0xdb:    case 0xdc: case 0xdd: case 0xde: case 0xdf:          case 0xe0: case 0xe1: case 0xe2: case 0xe3:    case 0xe4: case 0xe5: case 0xe6: case 0xe7:    case 0xe8: case 0xe9: case 0xea: case 0xeb:    case 0xec: case 0xed: case 0xee: case 0xef:      return Long.valueOf(tag - LONG_ZERO);      /* byte long */    case 0xf0: case 0xf1: case 0xf2: case 0xf3:    case 0xf4: case 0xf5: case 0xf6: case 0xf7:    case 0xf8: case 0xf9: case 0xfa: case 0xfb:    case 0xfc: case 0xfd: case 0xfe: case 0xff:      return Long.valueOf(((tag - LONG_BYTE_ZERO) << 8) + read());            /* short long */    case 0x38: case 0x39: case 0x3a: case 0x3b:    case 0x3c: case 0x3d: case 0x3e: case 0x3f:      return Long.valueOf(((tag - LONG_SHORT_ZERO) << 16) + 256 * read() + read());          case LONG_INT:      return Long.valueOf(parseInt());        case 'L':      return Long.valueOf(parseLong());    case DOUBLE_ZERO:      return Double.valueOf(0);    case DOUBLE_ONE:      return Double.valueOf(1);    case DOUBLE_BYTE:      return Double.valueOf((byte) read());    case DOUBLE_SHORT:      return Double.valueOf((short) (256 * read() + read()));          case DOUBLE_FLOAT:      {	int f = parseInt();	return Double.valueOf(Float.intBitsToFloat(f));      }    case 'D':      return Double.valueOf(parseDouble());        case 'd':      return new Date(parseLong());        case 'x':    case 'X': {      _isLastChunk = tag == 'X';      _chunkLength = (read() << 8) + read();      return parseXML();    }    case 's':    case 'S': {      _isLastChunk = tag == 'S';      _chunkLength = (read() << 8) + read();      int data;      _sbuf.setLength(0);            while ((data = parseChar()) >= 0)        _sbuf.append((char) data);      return _sbuf.toString();    }    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 = tag - 0x00;	int data;	_sbuf.setLength(0);      	while ((data = parseChar()) >= 0)	  _sbuf.append((char) data);	return _sbuf.toString();      }    case 'b':    case 'B': {      _isLastChunk = tag == 'B';      _chunkLength = (read() << 8) + read();      int data;      ByteArrayOutputStream bos = new ByteArrayOutputStream();            while ((data = parseByte()) >= 0)        bos.write(data);      return bos.toByteArray();    }    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;	int len = tag - 0x20;	_chunkLength = 0;	byte []data = new byte[len];	for (int i = 0; i < len; i++)	  data[i] = (byte) read();	return data;      }    case 'V': {      String type = readType();      int length = readLength();      return findSerializerFactory().readList(this, length, type);    }      // direct lists    case 'v': {      int ref = readInt();      String type = (String) _types.get(ref);      int length = readInt();            Deserializer reader;      reader = findSerializerFactory().getObjectDeserializer(type, null);            return reader.readLengthList(this, length);    }    case 'M': {      String type = readType();      return findSerializerFactory().readMap(this, type);    }    case 'O': {      readObjectDefinition(null);      return readObject();    }    case 'o': {      int ref = readInt();      ObjectDefinition def = (ObjectDefinition) _classDefs.get(ref);      return readObjectInstance(null, def);    }    case 'R': {      int ref = parseInt();      return _refs.get(ref);    }    case REF_BYTE: {      int ref = read();      return _refs.get(ref);    }    case REF_SHORT: {      int ref = 256 * read() + read();      return _refs.get(ref);    }          case 'r': {      String type = readType();      String url = readString();      return resolveRemote(type, url);    }    default:      if (tag < 0)	throw new EOFException("readObject: unexpected end of file");      else	throw error("readObject: unknown code " + codeName(tag));    }  }  /**   * Reads an object definition:   *   * <pre>   * O string <int> (string)* <value>*   * </pre>   */  private void readObjectDefinition(Class cl)    throws IOException  {    String type = readString();    int len = readInt();    String []fieldNames = new String[len];    for (int i = 0; i < len; i++)      fieldNames[i] = readString();    ObjectDefinition def = new ObjectDefinition(type, fieldNames);    if (_classDefs == null)      _classDefs = new ArrayList();    _classDefs.add(def);  }  private Object readObjectInstance(Class cl, ObjectDefinition def)    throws IOException  {    String type = def.getType();    String []fieldNames = def.getFieldNames();        if (cl != null) {      Deserializer reader;      reader = findSerializerFactory().getObjectDeserializer(type, cl);      return reader.readObject(this, fieldNames);    }    else {      return findSerializerFactory().readObject(this, type, fieldNames);    }  }  private String readLenString()    throws IOException  {    int len = readInt();        _isLastChunk = true;    _chunkLength = len;    _sbuf.setLength(0);    int ch;    while ((ch = parseChar()) >= 0)      _sbuf.append((char) ch);    return _sbuf.toString();  }  private String readLenString(int len)    throws IOException  {    _isLastChunk = true;    _chunkLength = len;    _sbuf.setLength(0);    int ch;    while ((ch = parseChar()) >= 0)      _sbuf.append((char) ch);    return _sbuf.toString();  }    /**   * Reads a remote object.   */  public Object readRemote()

⌨️ 快捷键说明

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