public Hessian2ObjectInput(InputStream is) { mH2i = new Hessian2Input(is); mH2i.setSerializerFactory(Hessian2SerializerFactory.SERIALIZER_FACTORY); }
/** * 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); } }
@Override public void close() throws IOException { Hessian2Input in = _in; _in = null; if (in != null) { _bodyIn.close(); int len = in.readInt(); byte[] signature = null; for (int i = 0; i < len; i++) { String header = in.readString(); if ("signature".equals(header)) signature = in.readBytes(); } in.completeEnvelope(); in.close(); if (signature == null) throw new IOException("Expected signature"); byte[] sig = _mac.doFinal(); if (sig.length != signature.length) throw new IOException("mismatched signature"); for (int i = 0; i < sig.length; i++) { if (signature[i] != sig[i]) throw new IOException("mismatched signature"); } // XXX: save principal } } }
/** * Reads a remote object. */ @Override public Object readRemote() throws IOException { String type = readType(); String url = readString(); return resolveRemote(type, url); }
DeflateInputStream(Hessian2Input in) throws IOException { _in = in; int len = in.readInt(); if (len != 0) throw new IOException("expected no headers"); _bodyIn = _in.readInputStream(); _inflateIn = new InflaterInputStream(_bodyIn); }
@Override public Object readObject(List<Class<?>> expectedTypes) throws IOException { int tag = _offset < _length ? (_buffer[_offset++] & 0xff) : read(); case 0xce: case 0xcf: return Integer.valueOf(((tag - BC_INT_BYTE_ZERO) << 8) + read()); case 0xd7: return Integer.valueOf(((tag - BC_INT_SHORT_ZERO) << 16) + 256 * read() + read()); return Integer.valueOf(parseInt()); case 0xfe: case 0xff: return Long.valueOf(((tag - BC_LONG_BYTE_ZERO) << 8) + read()); case 0x3e: case 0x3f: return Long.valueOf(((tag - BC_LONG_SHORT_ZERO) << 16) + 256 * read() + read()); return Long.valueOf(parseInt()); return Long.valueOf(parseLong()); return Double.valueOf((byte) read()); return Double.valueOf((short) (256 * read() + read()));
@Override public Object readObject(Class expectedClass, Class<?>... expectedTypes) throws IOException { if (expectedClass == null || expectedClass == Object.class) return readObject(); int tag = _offset < _length ? (_buffer[_offset++] & 0xff) : read(); Deserializer reader = findSerializerFactory().getDeserializer(expectedClass); String type = readType(); reader = findSerializerFactory().getDeserializer(expectedClass); reader = findSerializerFactory().getObjectDeserializer(type, expectedClass); readObjectDefinition(expectedClass); return readObject(expectedClass); return readObjectInstance(expectedClass, def); int ref = readInt(); int size = _classDefs.size(); return readObjectInstance(expectedClass, def); String type = readType(); reader = findSerializerFactory().getListDeserializer(type, expectedClass);
byte[] encKey = null; int len = in.readInt(); String header = in.readString(); fingerprint = in.readBytes(); else if ("key-algorithm".equals(header)) keyAlgorithm = in.readString(); else if ("algorithm".equals(header)) algorithm = in.readString(); else if ("key".equals(header)) encKey = in.readBytes(); else throw new IOException("'" + header + "' is an unexpected header"); _bodyIn = _in.readInputStream();
@Override public Hessian2Input unwrapHeaders(Hessian2Input in) throws IOException { InputStream is = new DeflateInputStream(in); Hessian2Input filter = new Hessian2Input(is); filter.setCloseStreamOnClose(true); return filter; }
@Override public void close() throws IOException { Hessian2Input in = _in; _in = null; if (in != null) { _inflateIn.close(); _bodyIn.close(); int len = in.readInt(); if (len != 0) throw new IOException("Unexpected footer"); in.completeEnvelope(); in.close(); } } }
@Override public float readFloat() throws IOException { return (float) mH2i.readDouble(); }
private Object deserializeObject(byte[] buf) throws IOException { ByteArrayInputStream is = new ByteArrayInputStream(buf); Hessian2Input hin = new Hessian2Input(is); Object object = hin.readObject(); hin.close(); is.close(); return object; }
@SuppressWarnings("unchecked") public static <T> T decode(byte[] data) throws IOException { Object obj = null; ByteArrayInputStream in = new ByteArrayInputStream(data); Hessian2Input hessian2Input = new Hessian2Input(in); hessian2Input.setSerializerFactory(serializerFactory); try { obj = hessian2Input.readObject(); } finally { closeableQuietly(in); closeQuietly(hessian2Input); } return (T) obj; }
/** * Reads an arbitrary object from the input stream when the type * is unknown. */ @Override public Object readObject() throws IOException { return readObject((List<Class<?>>) null); }
/** * 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; }
public String readType() throws IOException { int code = _offset < _length ? (_buffer[_offset++] & 0xff) : read(); _offset--; case BC_STRING_CHUNK: case 'S': { String type = readString(); int ref = readInt();
/** * Reads a short * <p> * <pre> * I b32 b24 b16 b8 * </pre> */ public short readShort() throws IOException { return (short) readInt(); }
@Override public byte[] readBytes() throws IOException { return mH2i.readBytes(); }
/** * Reads an object definition: * <p> * <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); }
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)); } } }