Example usage for java.io ByteArrayOutputStream write

List of usage examples for java.io ByteArrayOutputStream write

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream write.

Prototype

public synchronized void write(byte b[], int off, int len) 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this ByteArrayOutputStream .

Usage

From source file:com.github.jrialland.ajpclient.servlet.TestServletProxy.java

private static String slurp(final InputStream is) throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final byte[] buff = new byte[4096];
    int c = 0;//w  w w . j a v a2 s  . co m
    while ((c = is.read(buff)) > -1) {
        baos.write(buff, 0, c);
    }
    return baos.toString();
}

From source file:com.predic8.membrane.core.util.ByteUtil.java

public static byte[] getByteArrayData(InputStream stream) throws IOException {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    byte[] buffer = new byte[2048];
    while (true) {
        int read = stream.read(buffer);
        if (read < 0)
            break;
        bos.write(buffer, 0, read);
    }// ww  w  . j a  v  a 2 s.  c  o m

    try {
        bos.close();
    } catch (IOException e) {
        log.error("", e);
    }

    return bos.toByteArray();
}

From source file:controllers.user.UserAvatarApp.java

@Deprecated
public static Result render(File file) {
    try {//ww  w  .jav  a  2  s. c o m
        FileInputStream in = new FileInputStream(file);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] b = new byte[2048];
        int len = -1;
        while ((len = in.read(b)) != -1) {
            out.write(b, 0, len);
        }
        in.close();
        return ok(out.toByteArray());
    } catch (Exception e) {
        Logger.error("render user avatar error ", e);
    }
    return ok();
}

From source file:Main.java

public static byte[] readInputStreamAsByteArray(InputStream is) throws IOException {
    int available = is.available();
    if (available <= 0) {
        available = 32 * 1024;// ww w.j av a2s .  com
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream(available);

    byte[] buffer = new byte[32 * 1024];

    try {
        while (true) {

            int len = is.read(buffer);

            if (len <= 0) {

                break;
            }

            baos.write(buffer, 0, len);
        }

        return (baos.toByteArray());

    } finally {

        is.close();
    }
}

From source file:davmail.util.IOUtil.java

/**
 * Read all inputStream content to a byte array.
 *
 * @param inputStream input stream/*www  .j  a  va  2s  .co  m*/
 * @return content as byte array
 * @throws IOException on error
 */
public static byte[] readFully(InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] bytes = new byte[8192];
    int length;
    while ((length = inputStream.read(bytes)) > 0) {
        baos.write(bytes, 0, length);
    }
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] read(InputStream stream) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    try {//from w w w .  j a v  a 2  s . com
        byte[] buffer = new byte[1024];
        int length;

        while ((length = stream.read(buffer)) >= 0) {
            byteArrayOutputStream.write(buffer, 0, length);
        }
        stream.close();
        return byteArrayOutputStream.toByteArray();
    } finally {
        byteArrayOutputStream.flush();
        byteArrayOutputStream.close();

    }
}

From source file:StreamUtil.java

/**
 * Reads the InputStream and creates a byte[] of the content. The stream
 * will be closed at the end of operation.
 * @param is The input InputStream./*from   ww w . ja v a  2 s .  c  o m*/
 * @return The byte[] of the content read.
 * @throws IOException
 */
public static byte[] inputStream2Bytes(final InputStream is) throws IOException {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024 * 8];
        int len = -1;
        while ((len = is.read(buf)) != -1) {
            baos.write(buf, 0, len);
        }
        return baos.toByteArray(); // baos doesn't need close!
    } finally {
        is.close();
    }
}

From source file:de.intevation.irix.ImageClient.java

/** Obtains a Report from mapfish-print service.
 *
 * @param imageUrl The url to send the request to.
 * @param timeout the timeout for the httpconnection.
 *
 * @return byte[] with the report./*w w  w. ja v a 2 s  . com*/
 *
 * @throws IOException if communication with print service failed.
 * @throws ImageException if the print job failed.
 */
public static byte[] getImage(String imageUrl, int timeout) throws IOException, ImageException {

    RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout).build();

    CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(config).build();

    HttpGet get = new HttpGet(imageUrl);
    CloseableHttpResponse resp = client.execute(get);

    StatusLine status = resp.getStatusLine();

    byte[] retval = null;
    try {
        HttpEntity respEnt = resp.getEntity();
        InputStream in = respEnt.getContent();
        if (in != null) {
            try {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                byte[] buf = new byte[BYTE_ARRAY_SIZE];
                int r;
                while ((r = in.read(buf)) >= 0) {
                    out.write(buf, 0, r);
                }
                retval = out.toByteArray();
            } finally {
                in.close();
                EntityUtils.consume(respEnt);
            }
        }
    } finally {
        resp.close();
    }

    if (status.getStatusCode() < HttpURLConnection.HTTP_OK
            || status.getStatusCode() >= HttpURLConnection.HTTP_MULT_CHOICE) {
        if (retval != null) {
            throw new ImageException(new String(retval));
        } else {
            throw new ImageException("Communication with print service '" + imageUrl + "' failed."
                    + "\nNo response from print service.");
        }
    }
    return retval;
}

From source file:com.relurori.sandbox.spreadsheet.gdata.AbstractGetNameTask.java

/**
 * Reads the response from the input stream and returns it as a string.
 */// ww  w. ja va2s  .  c  om
private static String readResponse(InputStream is) throws IOException {
    Log.d(TAG, "readResponse");

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] data = new byte[2048];
    int len = 0;
    while ((len = is.read(data, 0, data.length)) >= 0) {
        bos.write(data, 0, len);
    }
    return new String(bos.toByteArray(), "UTF-8");
}

From source file:com.hat.tools.HttpUtils.java

public static void SendRequest(String url, Handler handler) {
    HttpGet request = new HttpGet(url);
    HttpClient httpClient = new DefaultHttpClient();

    byte[] data = null;
    InputStream in = null;//from ww w  . j av a  2 s  .c  om
    try {
        HttpResponse response = httpClient.execute(request);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            in = response.getEntity().getContent();
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = in.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            data = out.toByteArray();
            Message msg = new Message();
            msg.what = HTTP_FINISH;
            Bundle bundle = new Bundle();
            bundle.putByteArray("data", data);
            msg.setData(bundle);
            handler.sendMessage(msg);
            out.close();
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {

        try {
            if (in != null)
                in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}