/** 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); } }
private Object deserializeValue(SerializationDelegate serialization, Object storeValue) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream((byte[]) storeValue); try { return serialization.deserialize(in); } finally { in.close(); } }
@Override public int read(byte[] buffer, long offset, int length) throws ProxyCacheException { if (offset >= data.length) { return -1; } if (offset > Integer.MAX_VALUE) { throw new IllegalArgumentException("Too long offset for memory cache " + offset); } return new ByteArrayInputStream(data).read(buffer, (int) offset, length); }
private static void skipCondition(DataInputView in) throws IOException, ClassNotFoundException { boolean hasCondition = in.readBoolean(); if (hasCondition) { int length = in.readInt(); byte[] serCondition = new byte[length]; in.read(serCondition); ByteArrayInputStream bais = new ByteArrayInputStream(serCondition); ObjectInputStream ois = new ObjectInputStream(bais); ois.readObject(); ois.close(); bais.close(); } }
public StaticInputStream(InputStream input) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byteBuf = Unpooled.buffer(); // Fake code simulating the copy // You can generally do better with nio if you need... // And please, unlike me, do something about the Exceptions :D byte[] buffer = new byte[1024]; int len; while ((len = input.read(buffer)) > -1) { size += len; baos.write(buffer, 0, len); } baos.flush(); // Open new InputStreams using the recorded bytes // Can be repeated as many times as you wish this.inputStream = new ByteArrayInputStream(baos.toByteArray()); byteBuf.writeBytes(this.inputStream, size); }
@Override public KafkaConfig clone() { try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); SERIALIZER.serialize(this, new DataOutputStream(baos)); return SERIALIZER.deserialize(new DataInputStream(new ByteArrayInputStream(baos.toByteArray()))); } catch (IOException e) { throw new RuntimeException(e);//in mem, should not happen } }
ByteArrayOutputStream memoryStream = new ByteArrayOutputStream(); memoryStream.writeTo(urlOutStream); urlOutStream.flush(); urlOutStream.close(); memoryStream.close(); } else { PrintWriter writer = new PrintWriter(urlOutStream); urlOutStream.flush(); urlOutStream.close(); int data = serviceResponseStream.read(); if (-1 == data) { break; ByteArrayInputStream memoryStreamIn = new ByteArrayInputStream( memoryStream.toByteArray()); EwsXmlReader reader = new EwsXmlReader(memoryStreamIn); serviceResponseStream.close(); } finally { if (request != null) {
if((read = mReader.read(buffer, 0, MAX_REQUEST_SIZE)) > 0){ Profiler.instance().profile("proxy request parse"); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buffer, 0, read); BufferedReader bReader = new BufferedReader(new InputStreamReader(byteArrayInputStream)); StringBuilder builder = new StringBuilder(); mServerWriter.write(request.getBytes()); mServerWriter.flush(); mReader.close();
private void runStreamTest(int pLength) throws Exception { byte[] data = createData(pLength); ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); OutputStream out = new EncoderStream(outBytes, createCompatibleEncoder(), true); out.write(data); out.close(); byte[] encoded = outBytes.toByteArray(); byte[] decoded = FileUtil.read(new DecoderStream(new ByteArrayInputStream(encoded), createDecoder())); assertArrayEquals(String.format("Data %d", pLength), data, decoded); InputStream in = new DecoderStream(new ByteArrayInputStream(encoded), createDecoder()); outBytes = new ByteArrayOutputStream(); FileUtil.copy(in, outBytes); outBytes.close(); in.close(); decoded = outBytes.toByteArray(); assertArrayEquals(String.format("Data %d", pLength), data, decoded); }
public static byte[] decompress(byte[] compressed, int offset, int length) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); InputStream instream = new ByteArrayInputStream(compressed, offset, length); InputStream stream = new RLEDecompressingInputStream(instream); IOUtils.copy(stream, out); stream.close(); out.close(); return out.toByteArray(); } }
enum StringCompressor { ; public static byte[] compress(String text) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { OutputStream out = new DeflaterOutputStream(baos); out.write(text.getBytes("UTF-8")); out.close(); } catch (IOException e) { throw new AssertionError(e); } return baos.toByteArray(); } public static String decompress(byte[] bytes) { InputStream in = new InflaterInputStream(new ByteArrayInputStream(bytes)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { byte[] buffer = new byte[8192]; int len; while((len = in.read(buffer))>0) baos.write(buffer, 0, len); return new String(baos.toByteArray(), "UTF-8"); } catch (IOException e) { throw new AssertionError(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); }
DataInputStream diffIn = new DataInputStream(new ByteArrayInputStream(diffBuf, 0, diffSize)); diffIn.close(); InputStream in = new ByteArrayInputStream(diffBuf, 0, diffSize); in.skip(BSUtil.HEADER_SIZE); DataInputStream ctrlBlockIn = new DataInputStream(new GZIPInputStream(in)); in = new ByteArrayInputStream(diffBuf, 0, diffSize); in.skip(ctrlBlockLen + BSUtil.HEADER_SIZE); InputStream diffBlockIn = new GZIPInputStream(in); in = new ByteArrayInputStream(diffBuf, 0, diffSize); in.skip(diffBlockLen + ctrlBlockLen + BSUtil.HEADER_SIZE); InputStream extraBlockIn = new GZIPInputStream(in); outStream.close(); return RETURN_DIFF_FILE_ERR; outStream.close(); return RETURN_DIFF_FILE_ERR; outStream.close(); return RETURN_DIFF_FILE_ERR;
public static final void testSerialization(String[] values) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(4096); DataOutputStream serializer = new DataOutputStream(baos); for (String value : values) { StringValue.writeString(value, serializer); } serializer.close(); baos.close(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); DataInputStream deserializer = new DataInputStream(bais); int num = 0; while (deserializer.available() > 0) { String deser = StringValue.readString(deserializer); assertEquals("DeserializedString differs from original string.", values[num], deser); num++; } assertEquals("Wrong number of deserialized values", values.length, num); }
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()); }
private InputStream getMemInputStream(JarFile jf, JarEntry je) throws IOException { InputStream is = getInputStream4336753(jf, je); ByteArrayOutputStream os = new ByteArrayOutputStream(is.available()); try { FileUtil.copy(is, os); } finally { os.close(); } return new ByteArrayInputStream(os.toByteArray()); }
@Override @Before public void setup() throws IOException { original = new MoreComplexObject(); final ByteArrayOutputStream bos = willClose(new ByteArrayOutputStream()); final ObjectOutputStream oos = willClose(new ObjectOutputStream(bos)); oos.writeObject(original); inputStream = willClose(new ByteArrayInputStream(bos.toByteArray())); }
public void testSkipBytes() throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(baos); /* Write out various test values NORMALLY */ out.write(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); // 10 bytes of junk to skip initializeData(out); byte[] data = baos.toByteArray(); DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while (bytesSkipped < 10) { bytesSkipped += in.skipBytes(10 - bytesSkipped); } /* Read in various values in LITTLE ENDIAN FORMAT */ byte[] b = new byte[2]; in.readFully(b); assertEquals(-100, b[0]); assertEquals(100, b[1]); assertTrue(in.readBoolean()); assertFalse(in.readBoolean()); } }
@Test public void copyToString() throws Exception { Charset charset = Charset.defaultCharset(); InputStream inputStream = spy(new ByteArrayInputStream(string.getBytes(charset))); String actual = StreamUtils.copyToString(inputStream, charset); assertThat(actual, equalTo(string)); verify(inputStream, never()).close(); }
public void writeTo(OutputStream out, int buflen) throws IOException { byte[] buf = new byte[buflen]; int read = 0; InputStream sinkReader = new BufferedInputStream( new ByteArrayInputStream( ((ByteArrayOutputStream)this.out).toByteArray()), buflen); while ( ( read = sinkReader.read(buf) ) != -1 ) { out.write(buf, 0, read); } }