@SuppressWarnings("unchecked") public <L extends GeneratedMessage> L get(Class<L> clazz, ExtensionRegistryLite registry) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Method parseMethod = clazz.getDeclaredMethod("parseFrom", InputStream.class, ExtensionRegistryLite.class); return (L) parseMethod.invoke(null, input.asInputStream(), registry); }
@SuppressWarnings("unchecked") @Override public IN decode(IoBuffer input) { try { return (IN) parseMethod.invoke(null, input.asInputStream(), registry); } catch (Exception e) { throw new ProtocolDecoderException(e); } } }
@SuppressWarnings("unchecked") @Override public IN decode(final IoBuffer input) { try { ObjectInputStream ois = new ObjectInputStream(input.asInputStream()); IN s = (IN) ois.readObject(); ois.close(); return s; } catch (Exception ex) { throw new ProtocolDecoderException(ex); } } }
@Test public void testInputStreamGetByte() throws IOException { String hw = "HelloWorld"; IoBuffer bb = IoBuffer.wrap(hw.getBytes()); InputStream is = bb.asInputStream(); for (int i = 0; i < 10; i++) { assertEquals(i, bb.position()); assertEquals(hw.getBytes()[i], is.read()); } assertEquals(-1, is.read()); }
@Test public void testInputStreamGetByteArray() throws IOException { String hw = "HelloWorld"; IoBuffer bb = IoBuffer.wrap(hw.getBytes()); InputStream is = bb.asInputStream(); byte array[] = new byte[15]; assertEquals(5, is.read(array, 0, 5)); assertEquals(5, bb.position()); assertEquals(5, is.read(array, 5, 10)); assertEquals(10, bb.position()); for (int i = 0; i < 10; i++) { assertEquals(hw.getBytes()[i], array[i]); } }