@Override public InputStream getContent() throws IOException, IllegalStateException { if (this.objSer == null) { createBytes(this.objRef); } return new ByteArrayInputStream(this.objSer); }
@Test public void testBasicsDirect() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ObjectOutputStream out = new ObjectOutputStream(baos); final Serializable serializableObj = new SerializableObject(); out.writeObject(serializableObj); final SerializableEntity httpentity = new SerializableEntity(serializableObj, false); Assert.assertEquals(-1, httpentity.getContentLength()); Assert.assertNotNull(httpentity.getContent()); Assert.assertTrue(httpentity.isRepeatable()); Assert.assertFalse(httpentity.isStreaming()); }
@Test public void testWriteToBuff() throws Exception { final Serializable serializableObj = new SerializableObject(); final SerializableEntity httpentity = new SerializableEntity(serializableObj, true); final ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.writeTo(out); final byte[] bytes = out.toByteArray(); Assert.assertNotNull(bytes); final ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream( bytes)); final SerializableObject serIn = (SerializableObject) oin.readObject(); Assert.assertEquals(4, serIn.intValue); Assert.assertEquals("Hello", serIn.stringValue); try { httpentity.writeTo(null); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { // expected } }
@Test public void testIllegalConstructor() throws Exception { try { new SerializableEntity(null, false); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { // expected } }
@Test public void testBasicsBuff() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ObjectOutputStream out = new ObjectOutputStream(baos); final Serializable serializableObj = new SerializableObject(); out.writeObject(serializableObj); final SerializableEntity httpentity = new SerializableEntity(serializableObj, true); Assert.assertEquals(baos.toByteArray().length, httpentity.getContentLength()); Assert.assertNotNull(httpentity.getContent()); Assert.assertTrue(httpentity.isRepeatable()); Assert.assertFalse(httpentity.isStreaming()); }
@Test public void testWriteToDirect() throws Exception { final Serializable serializableObj = new SerializableObject(); final SerializableEntity httpentity = new SerializableEntity(serializableObj, false); final ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.writeTo(out); final byte[] bytes = out.toByteArray(); Assert.assertNotNull(bytes); final ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream( bytes)); final SerializableObject serIn = (SerializableObject) oin.readObject(); Assert.assertEquals(4, serIn.intValue); Assert.assertEquals("Hello", serIn.stringValue); try { httpentity.writeTo(null); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { // expected } }
this.contentType != null ? this.contentType.getCharset() : null); } else if (this.serializable != null) { e = new SerializableEntity(this.serializable); e.setContentType(ContentType.DEFAULT_BINARY.toString()); } else if (this.file != null) {
@Override public InputStream getContent() throws IOException, IllegalStateException { if (this.objSer == null) { createBytes(this.objRef); } return new ByteArrayInputStream(this.objSer); }
/** * Creates new instance of this class. * * @param ser input * @param bufferize tells whether the content should be * stored in an internal buffer * @throws IOException in case of an I/O error */ public SerializableEntity(final Serializable ser, final boolean bufferize) throws IOException { super(); Args.notNull(ser, "Source object"); if (bufferize) { createBytes(ser); } else { this.objRef = ser; } }
/** * Creates new instance of this class. * * @param ser input * @param bufferize tells whether the content should be * stored in an internal buffer * @throws IOException in case of an I/O error */ public SerializableEntity(final Serializable ser, final boolean bufferize) throws IOException { super(); Args.notNull(ser, "Source object"); if (bufferize) { createBytes(ser); } else { this.objRef = ser; } }