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(int b) 

Source Link

Document

Writes the specified byte to this ByteArrayOutputStream .

Usage

From source file:Main.java

public static String convertStreamToString(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i = is.read();
    while (i != -1) {
        baos.write(i);
        i = is.read();//from   w  w  w.j a  v a 2 s.  c o m
    }
    return baos.toString();
}

From source file:Main.java

public static String readStream(InputStream in) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i;/*from  ww  w  .  java 2s . co  m*/
    while ((i = in.read()) != -1) {
        baos.write(i);
    }
    return baos.toString();
}

From source file:Main.java

public static String inputStream2String(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i = -1;/*from   w ww  .  j a v a 2  s .  c  o  m*/
    while ((i = is.read()) != -1) {
        baos.write(i);
    }
    return baos.toString();
}

From source file:Main.java

public static byte[] decompressGZIP(byte bytes[]) throws IOException {
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    GZIPInputStream gzipis = new GZIPInputStream(is);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    int i;/*from ww  w .j a  va  2 s  .c o m*/
    while ((i = gzipis.read()) != -1) {
        os.write(i);
    }
    gzipis.close();
    os.close();
    return os.toByteArray();
}

From source file:Main.java

static void writeInt(ByteArrayOutputStream baos, int i) {
    for (int j = 0; j < 4; j++) {
        int shift = 24 - j * 8;
        baos.write((byte) (i >>> shift));
    }/*www  .j av  a2 s  .c  o  m*/
}

From source file:Main.java

public static byte[] getPrintBarCode(String data, int symbology, int height, int width, int textposition) {

    if (symbology < 0 || symbology > 8) {
        return new byte[] { 0x0A };
    }/*  w ww. j a  va2s .c  o m*/
    if (width < 2 || width > 6) {
        width = 2;
    }
    if (textposition < 0 || textposition > 3) {
        textposition = 0;
    }
    if (height < 1 || height > 255) {
        height = 162;
    }

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    try {
        buffer.write(new byte[] { 0x1D, 0x66, 0x01, 0x1D, 0x48, (byte) textposition, 0x1D, 0x77, (byte) width,
                0x1D, 0x68, (byte) height, 0x0A });

        byte[] barcode = data.getBytes();

        if (symbology == 8) {
            buffer.write(new byte[] { 0x1D, 0x6B, 0x49, (byte) (barcode.length + 2), 0x7B, 0x42 });
        } else {
            buffer.write(new byte[] { 0x1D, 0x6B, (byte) (symbology + 0x41), (byte) barcode.length });
        }

        buffer.write(barcode);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return buffer.toByteArray();
}

From source file:Main.java

public static String getStringFromAssets(String strFileName, Resources resources) {
    String result = null;/*  w  w w .  j a va2 s.  c o m*/
    try {
        InputStream in = resources.getAssets().open(strFileName);
        int ch = 0;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((ch = in.read()) != -1) {
            baos.write(ch);
        }
        byte[] buff = baos.toByteArray();
        baos.close();
        in.close();
        result = new String(buff);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static byte[] InputStreamToByte(InputStream is) throws IOException {

    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
    int ch;/*from   w  w  w  . j  ava  2 s. c om*/
    while ((ch = is.read()) != -1) {
        bytestream.write(ch);
    }
    byte byteData[] = bytestream.toByteArray();
    bytestream.close();
    return byteData;
}

From source file:Main.java

public static byte[] InputStreamToByte(InputStream is) throws IOException {
    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
    int ch;//from   w w  w  .j a v a2s . co m
    while ((ch = is.read()) != -1) {
        bytestream.write(ch);
    }
    byte imgdata[] = bytestream.toByteArray();
    bytestream.close();

    return imgdata;
}

From source file:Main.java

public static byte[] getBytesFromInput(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[in.available()];
    in.read(buffer);/*w w w  .  java2 s .co m*/
    out.write(buffer);
    return out.toByteArray();
}