/** * Reads the start of a list. */ @Override public int readListStart() throws IOException { return read(); }
/** * 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 a reply as an object. * If the reply has a fault, throws the exception. */ @Override public Object readReply(Class expectedClass) throws Throwable { int tag = read(); if (tag == 'R') return readObject(expectedClass); else if (tag == 'F') { HashMap map = (HashMap) readObject(HashMap.class); throw prepareFault(map); } else { StringBuilder sb = new StringBuilder(); sb.append((char) tag); try { int ch; while ((ch = read()) >= 0) { sb.append((char) ch); } } catch (IOException e) { log.log(Level.FINE, e.toString(), e); } throw error("expected hessian reply at " + codeName(tag) + "\n" + sb); } }
/** * Reads the start of a list. */ @Override public int readMapStart() throws IOException { return read(); }
@Override 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; }
/** * Returns true if this is the end of a list or a map. */ @Override 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'); }
/** * Parses a 32-bit integer value from the stream. * <p> * <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. * <p> * <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 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)); }
/** * Starts reading the envelope * <p> * <pre> * E major minor * </pre> */ public int readEnvelope() throws IOException { int tag = read(); int version = 0; if (tag == 'H') { int major = read(); int minor = read(); version = (major << 16) + minor; tag = read(); } if (tag != 'E') throw error("expected hessian Envelope ('E') at " + codeName(tag)); return version; }
/** * Starts reading the message * <p> * <pre> * p major minor * </pre> */ public int startMessage() throws IOException { int tag = read(); if (tag == 'p') _isStreaming = false; else if (tag == 'P') _isStreaming = true; else throw error("expected Hessian message ('p') at " + codeName(tag)); int major = read(); int minor = read(); return (major << 16) + minor; }
/** * Reads a null * <p> * <pre> * N * </pre> */ @Override public void readNull() throws IOException { int tag = read(); switch (tag) { case 'N': return; default: throw expect("null", tag); } }
/** * Reads the end byte. */ @Override 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) + "'"); }
/** * Reads the end byte. */ @Override 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) + "'"); }
/** * Starts reading the call * <p> * <pre> * c major minor * </pre> */ @Override public int readCall() throws IOException { int tag = read(); if (tag != 'C') throw error("expected hessian call ('C') at " + codeName(tag)); return 0; }
/** * Completes reading the call * <p> * <p>A successful completion will have a single value: * <p> * <pre> * z * </pre> */ public void completeValueReply() throws IOException { int tag = read(); if (tag != 'Z') error("expected end of reply at " + codeName(tag)); }
/** * Completes reading the envelope * <p> * <p>A successful completion will have a single value: * <p> * <pre> * Z * </pre> */ public void completeEnvelope() throws IOException { int tag = read(); if (tag != 'Z') error("expected end of envelope at " + codeName(tag)); }
/** * Completes reading the message * <p> * <p>A successful completion will have a single value: * <p> * <pre> * z * </pre> */ public void completeMessage() throws IOException { int tag = read(); if (tag != 'Z') error("expected end of message at " + codeName(tag)); }
/** * Reads the end byte. */ @Override 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 a date. * <p> * <pre> * T b64 b56 b48 b40 b32 b24 b16 b8 * </pre> */ @Override public long readUTCDate() throws IOException { int tag = read(); if (tag == BC_DATE) { return parseLong(); } else if (tag == BC_DATE_MINUTE) { return parseInt() * 60000L; } else throw expect("date", tag); }