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:Main.java

public static byte[] readStream(InputStream in) throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = in.read(buffer)) != -1) {
        outputStream.write(buffer, 0, len);
    }/*  w  ww . jav  a2s . co  m*/
    outputStream.close();
    in.close();
    return outputStream.toByteArray();
}

From source file:com.polivoto.networking.ServicioDeIPExterna.java

public static String obtenerIPExterna() {
    String ip = null;//from   w  w w  .  j  av a2 s  .c om
    try {
        HttpURLConnection con = (HttpURLConnection) new URL(GET_EXTERNAL_HOST).openConnection();
        DataInputStream entrada = new DataInputStream(con.getInputStream());
        int length;
        byte[] chunk = new byte[64];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((length = entrada.read(chunk)) != -1)
            baos.write(chunk, 0, length);
        ip = baos.toString();
        baos.close();
        entrada.close();
        con.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("IP exterior: " + ip);
    return ip;
}

From source file:Main.java

public static byte[] inputStream2ByteArray(InputStream in) throws Exception {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[BYTE_ARRAY_LENTH];
    int len = -1;
    while ((len = in.read(buf)) != -1) {
        bos.write(buf, 0, len);
    }/*  w ww.j av  a2 s  . co  m*/

    return bos.toByteArray();
}

From source file:com.polivoto.networking.ServicioDeIPExterna.java

public static String obtenerIPServidorRemoto() {
    String ip = null;/*from  w w w  .  ja v a 2s.c o m*/
    try {
        HttpURLConnection con = (HttpURLConnection) new URL(REMOTE_HOST).openConnection();
        DataInputStream entrada = new DataInputStream(con.getInputStream());
        int length;
        byte[] chunk = new byte[64];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((length = entrada.read(chunk)) != -1)
            baos.write(chunk, 0, length);
        JSONObject json = new JSONObject(baos.toString());
        baos.close();
        entrada.close();
        con.disconnect();
        ip = json.getString("content");
    } catch (JSONException | IOException e) {
        e.printStackTrace();
    }
    System.out.println("IP servidor remoto: " + ip);
    return ip;
}

From source file:Main.java

public static ImageIcon iconFromStream(final InputStream in) throws IOException {
    if (in == null) {
        throw new IllegalArgumentException("Stream must not be null");
    }/*from  ww  w  .j  a  va  2 s  .  c  om*/
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) > 0) {
        out.write(buffer, 0, read);
    }
    in.close();
    return new ImageIcon(out.toByteArray());
}

From source file:Main.java

public static byte[] getBytes(InputStream inStream) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;/*from   ww w. ja v  a  2 s.  c  om*/
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }
    outStream.close();
    inStream.close();
    return outStream.toByteArray();
}

From source file:Main.java

public static String readData(InputStream inSream, String charsetName) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = inSream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }//  w  ww .  j av a2 s .c o m
    byte[] data = outStream.toByteArray();
    outStream.close();
    inSream.close();
    return new String(data, charsetName);
}

From source file:Main.java

public static byte[] readStream(InputStream inStream) throws Exception {
    ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = inStream.read(buffer)) != -1) {
        outSteam.write(buffer, 0, len);
    }//from ww  w  .j ava  2  s .c  om
    outSteam.close();
    inStream.close();
    return outSteam.toByteArray();
}

From source file:Main.java

public static final byte[] unCompress(byte[] buf) throws IOException {
    GZIPInputStream gzi = new GZIPInputStream(new ByteArrayInputStream(buf));
    ByteArrayOutputStream bos = new ByteArrayOutputStream(buf.length);

    int count;//from www .j  a  va2  s.c  o  m
    byte[] tmp = new byte[2048];
    while ((count = gzi.read(tmp)) != -1) {
        bos.write(tmp, 0, count);
    }

    // store uncompressed back to buffer      
    gzi.close();
    return bos.toByteArray();
}

From source file:Main.java

public static byte[] readStream(InputStream inStream) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;/*  w  w  w.  j  a v a2s. com*/
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }
    outStream.close();
    inStream.close();
    return outStream.toByteArray();
}