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();
@Override public String convert(Properties source) { try { ByteArrayOutputStream os = new ByteArrayOutputStream(256); source.store(os, null); return os.toString("ISO-8859-1"); catch (IOException ex) { // Should never happen. throw new IllegalArgumentException("Failed to store [" + source + "] into String", ex);
URL url = new URL("http://www.yahoo.com/image_to_read.jpg"); InputStream in = new BufferedInputStream(url.openStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int n = 0; while (-1!=(n=in.read(buf))) out.write(buf, 0, n); out.close(); in.close(); byte[] response = out.toByteArray();
ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); return result.toString("UTF-8");
private Object serializeValue(SerializationDelegate serialization, Object storeValue) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
serialization.serialize(storeValue, out);
return out.toByteArray();
finally {
out.close();
/** 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 void run() { try { for(int i=0;i<10;i++) { URL url = new URL("http://192.168.220.130/index.html"); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); InputStream in = conn.getInputStream(); ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte[] buff = new byte[1024]; int len = 0; while((len = in.read(buff)) >= 0) { bout.write(buff, 0, len); in.close(); bout.close(); byte[] response = bout.toByteArray(); System.out.println(new String(response, "UTF-8")); Thread.sleep(3000); }catch(Exception e) {
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();
in = url.openStream();
out = new FileOutputStream(tmpFile);
if (TRY_TO_PATCH_SHADED_ID && PlatformDependent.isOsx() && !packagePrefix.isEmpty()) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(in.available());
while ((length = in.read(buffer)) > 0) {
byteArrayOutputStream.write(buffer, 0, length);
byteArrayOutputStream.flush();
byte[] bytes = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
out.write(bytes);
} else {
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
out.flush();
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(ssf);
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = inputStream.read(buffer))) {
output.write(buffer, 0, n);
return output.toByteArray();
} catch (Exception e) {
e.printStackTrace();
protected byte[] doInBackground(String... params) {
try {
InputStream is = new URL(mUrl).openStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
buffer.flush();
return buffer.toByteArray();
} catch (IOException e) {
e.printStackTrace();
return null;
private static void doTestSerializationRoundTrip(DownloadAction action) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(out);
DownloadAction.serializeToStream(action, output);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
DataInputStream input = new DataInputStream(in);
DownloadAction action2 =
DownloadAction.deserializeFromStream(
new DownloadAction.Deserializer[] {DashDownloadAction.DESERIALIZER}, input);
assertThat(action).isEqualTo(action2);
buffer = new ByteArrayOutputStream();
while ((nRead = in.read(data, 0, BUFFER_SIZE)) != -1) {
buffer.write(data, 0, nRead);
buffer.flush();
return buffer.toByteArray();
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (in != null) try { in.close(); } catch (Exception ignored) {}
if (buffer != null) try { buffer.close(); } catch (Exception ignored) {}
public void testCreateAfterCloseShouldFail() throws Exception {
for (int i = 0; i < 10; i++) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
Ids.OPEN_ACL_UNSAFE, 1);
createReq.serialize(boa, "request");
baos.close();
System.out.println("Length:" + baos.toByteArray().length);
try {
OutputStream outstream = sock.getOutputStream();
byte[] data = baos.toByteArray();
outstream.write(data);
outstream.flush();
while ((len = resultStream.read(b)) >= 0) {
resultStream.close();
public static Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
oos.flush();
baos.flush();
byte[] bytes = baos.toByteArray();
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(is);
Object o2 = ois.readObject();
return o2;
@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);
public byte[] toByteArray() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
List<Object> all = new LinkedList<>();
synchronized (outList) {
all.addAll(outList);
for (Object o : all) {
if (o instanceof File) {
File f = (File) o;
FileInputStream fin = new FileInputStream(f);
copyStream(fin, out);
fin.close();
} else if (o instanceof byte[]) {
out.write((byte[]) o);
} else if (o instanceof Integer) {
out.write((int) o);
} else if (o instanceof URL) {
InputStream fin = ((URL) o).openStream();
copyStream(fin, out);
fin.close();
} else {
// can not handle the object
out.close();
return out.toByteArray();
@Override public byte[] enquireTerminal(int timeout, TimeUnit timeoutTimeUnit) throws IOException { synchronized(terminalOutput) { terminalOutput.write(5); //ENQ flush(); //Wait for input long startTime = System.currentTimeMillis();