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[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);/*  w ww. j  a  v  a2 s . c o  m*/

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();

    System.out.println("Original: " + data.length);
    System.out.println("Compressed: " + output.length);
    return output;
}

From source file:Main.java

private static byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);/*  w w  w. j  ava2  s . com*/

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();
    inflater.end();

    // System.out.println("Original: " + data.length + " bytes.");
    // System.out.println("Decompressed: " + output.length + " bytes.");
    return output;
}

From source file:Main.java

public static String streamToString(InputStream inputStream) {
    try {/*from   ww  w  . j a  v  a2 s. com*/
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            out.write(buffer, 0, len);
            out.flush();
        }

        String result = out.toString();
        out.close();
        inputStream.close();
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static final byte[] input2byte(InputStream inStream) {
    if (inStream == null)
        return null;
    ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
    byte[] buff = new byte[100];
    int rc = 0;//from   w  w w .j  a  v  a2  s.  co m
    try {
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return swapStream.toByteArray();
}

From source file:Main.java

private static byte[] convertInputStreamToByteArray(InputStream inputStream) throws IOException {
    byte[] bytes = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte data[] = new byte[1024];
    int count;/*from ww w.  ja va2s .  c  om*/
    while ((count = inputStream.read(data)) != -1) {
        bos.write(data, 0, count);
    }
    bos.flush();
    bos.close();
    inputStream.close();
    bytes = bos.toByteArray();
    return bytes;
}

From source file:Main.java

public static final byte[] input2byte(InputStream inStream) {
    if (inStream == null)
        return null;
    ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
    byte[] buff = new byte[100];
    int rc = 0;/*from  ww  w  .j a v  a 2  s.com*/
    try {
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    byte[] in2b = swapStream.toByteArray();
    return in2b;
}

From source file:Main.java

/**
 * uncompress the xmlByteArray//from  ww w.  j a va2 s.  c om
 */
public static byte[] uncompressByteArray(byte[] xmlByteArray) throws IOException {
    byte[] tmp = new byte[2048];
    int byteCount = 0;
    ByteArrayOutputStream uncompressedData = new ByteArrayOutputStream();
    GZIPInputStream gzipIS = new GZIPInputStream(new ByteArrayInputStream(xmlByteArray));

    while ((byteCount = gzipIS.read(tmp)) != -1) {
        uncompressedData.write(tmp, 0, byteCount);
    }

    return uncompressedData.toByteArray();
}

From source file:Util.java

/**
 * @param originalString Original character string
 * @param byteLength A necessary byte length
 * @param charset Because we use DB2, we fix "UTF-8".
 * @return/* w  ww  .ja  v  a 2  s  .  com*/
 * @throws UnsupportedEncodingException
 */
public static String getTruncatedString(String originalString, int byteLength, String charset)
        throws UnsupportedEncodingException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] originalBytes = originalString.getBytes(charset);
    if (originalBytes.length <= byteLength)
        return originalString;
    out.write(originalBytes, 0, byteLength + 1);
    String result = new String(out.toByteArray(), charset);
    return result.substring(0, result.length() - 1);
}

From source file:Main.java

public static String readInStream(InputStream in) {
    try {//from   ww  w. j  a v a  2 s  . co  m
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[512];
        int length = -1;
        while ((length = in.read(buffer)) != -1) {
            out.write(buffer, 0, length);
        }
        out.close();
        in.close();
    } catch (IOException e) {
        Log.e("FileTest", e.getMessage());
    }
    return null;
}

From source file:Main.java

public static String readFully(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length = 0;
    while ((length = is.read(buffer)) != -1) {
        baos.write(buffer, 0, length);
    }/*from   ww  w. jav  a  2  s .c  o m*/
    return baos.toString("UTF-8");
}