public static byte[] isToBytes(InputStream is) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int read; try { while ((read = is.read(buffer)) != -1) { os.write(buffer, 0, read); } } finally { is.close(); } return os.toByteArray(); }
/** Serializes and deserializes the specified object. */ @SuppressWarnings("unchecked") static <T> T reserialize(T object) { checkNotNull(object); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); try { ObjectOutputStream out = new ObjectOutputStream(bytes); out.writeObject(object); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray())); return (T) in.readObject(); } catch (IOException | ClassNotFoundException e) { throw new RuntimeException(e); } }
public static byte[] readNullTerminatedBytes(byte[] data, int index) { ByteArrayOutputStream out = new ByteArrayOutputStream(); for (int i = index; i < data.length; i++) { byte item = data[i]; if (item == MSC.NULL_TERMINATED_STRING_DELIMITER) { break; } out.write(item); } return out.toByteArray(); }
private Object serializeValue(SerializationDelegate serialization, Object storeValue) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { serialization.serialize(storeValue, out); return out.toByteArray(); } finally { out.close(); } }
private void transformAndMarshal(Object graph, Result result) throws IOException { try { ByteArrayOutputStream os = new ByteArrayOutputStream(1024); marshalOutputStream(graph, os); ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); Transformer transformer = this.transformerFactory.newTransformer(); transformer.transform(new StreamSource(is), result); } catch (TransformerException ex) { throw new MarshallingFailureException( "Could not transform to [" + ClassUtils.getShortName(result.getClass()) + "]", ex); } }
@Override public NFAState copy(NFAState from) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); serialize(from, new DataOutputViewStreamWrapper(baos)); baos.close(); byte[] data = baos.toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream(data); NFAState copy = deserialize(new DataInputViewStreamWrapper(bais)); bais.close(); return copy; } catch (IOException e) { throw new RuntimeException("Could not copy NFA.", e); } }
@SuppressWarnings("deprecation") // testing a deprecated method public void testWriteBytes() throws IOException { /* Write out various test values in LITTLE ENDIAN FORMAT */ out.writeBytes("r\u00C9sum\u00C9"); byte[] data = baos.toByteArray(); /* Setup input streams */ DataInput in = new DataInputStream(new ByteArrayInputStream(data)); /* Read in various values NORMALLY */ byte[] b = new byte[6]; in.readFully(b); assertEquals("r\u00C9sum\u00C9".getBytes(Charsets.ISO_8859_1), b); }
@Override protected void setUp() throws Exception { super.setUp(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos); initializeData(out); data = baos.toByteArray(); }
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = null; try { out = new ObjectOutputStream(bos); out.writeObject(yourObject); out.flush(); byte[] yourBytes = bos.toByteArray(); ... } finally { try { bos.close(); } catch (IOException ex) { // ignore close exception } }
/** * Creates the wrapper that be used in the serialization stream. * * <p>Immediately serializes the Mockito mock using specifically crafted {@link MockitoMockObjectOutputStream}, * in a byte array.</p> * * @param mockitoMock The Mockito mock to serialize. * @throws java.io.IOException */ public CrossClassLoaderSerializationProxy(Object mockitoMock) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new MockitoMockObjectOutputStream(out); objectOutputStream.writeObject(mockitoMock); objectOutputStream.close(); out.close(); MockCreationSettings<?> mockSettings = MockUtil.getMockSettings(mockitoMock); this.serializedMock = out.toByteArray(); this.typeToMock = mockSettings.getTypeToMock(); this.extraInterfaces = mockSettings.getExtraInterfaces(); }
private static byte[] readAllBytes(InputStream input) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); int numRead; byte[] buffer = new byte[16384]; while ((numRead = input.read(buffer, 0, buffer.length)) != -1) { out.write(buffer, 0, numRead); } return out.toByteArray(); }
@SuppressWarnings("unchecked") static <T> T reserialize(T object) { try { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bytes); out.writeObject(object); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray())); return (T) in.readObject(); } catch (IOException | ClassNotFoundException e) { Helpers.fail(e, e.getMessage()); } throw new AssertionError("not reachable"); }
/** * {@inheritDoc} */ public Sink write(Manifest manifest) throws IOException { if (manifest != null) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { manifest.write(outputStream); } finally { outputStream.close(); } storage.put(JarFile.MANIFEST_NAME, outputStream.toByteArray()); } return this; }
private String readCommand(ByteBuffer byteBuffer) { ByteArrayOutputStream command = new ByteArrayOutputStream(256); while (byteBuffer.remaining() > 0 && !tryConsumeEndOfLine(byteBuffer)) { command.write(byteBuffer.get()); } return new String(command.toByteArray(), StandardCharsets.UTF_8); }
public void testCopyChannel() throws IOException { byte[] expected = newPreFilledByteArray(100); ByteArrayOutputStream out = new ByteArrayOutputStream(); WritableByteChannel outChannel = Channels.newChannel(out); ReadableByteChannel inChannel = Channels.newChannel(new ByteArrayInputStream(expected)); ByteStreams.copy(inChannel, outChannel); assertEquals(expected, out.toByteArray()); }
@Test public void copyRange() throws Exception { ByteArrayOutputStream out = spy(new ByteArrayOutputStream()); StreamUtils.copyRange(new ByteArrayInputStream(bytes), out, 0, 100); byte[] range = Arrays.copyOfRange(bytes, 0, 101); assertThat(out.toByteArray(), equalTo(range)); verify(out, never()).close(); }
@SuppressWarnings("deprecation") // testing a deprecated method public void testWriteBytes_discardHighOrderBytes() throws IOException { /* Write out various test values in LITTLE ENDIAN FORMAT */ out.writeBytes("\uAAAA\uAABB\uAACC"); byte[] data = baos.toByteArray(); /* Setup input streams */ DataInput in = new DataInputStream(new ByteArrayInputStream(data)); /* Read in various values NORMALLY */ byte[] b = new byte[3]; in.readFully(b); byte[] expected = {(byte) 0xAA, (byte) 0xBB, (byte) 0xCC}; assertEquals(expected, b); }
public void testCopyTo_outputStream() throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); source.copyTo(out); assertExpectedBytes(out.toByteArray()); }
private static byte[] readAllBytes(InputStream input) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); int numRead; byte[] buffer = new byte[16384]; while ((numRead = input.read(buffer, 0, buffer.length)) != -1) { out.write(buffer, 0, numRead); } return out.toByteArray(); }
public static String serialize(Session session) { if (null == session) { return null; } try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(session); return Base64.encodeToString(bos.toByteArray()); } catch (Exception e) { throw new RuntimeException("serialize session error", e); } }