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 readFromAssets(Context context, String fileName) throws IOException {

    InputStream inputStream = context.getAssets().open(fileName);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    int i;/*from   ww  w .  j  av a2 s  . com*/
    try {
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return byteArrayOutputStream.toString();
}

From source file:Main.java

public static byte[] getByte(String ss) {
    String[] shil = ss.split(" ");
    StringBuilder er = new StringBuilder();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    for (String string : shil) {
        if ("".equals(string)) {
            continue;
        }//from   w w w  . j a v  a2 s. c  om
        int i = Integer.valueOf(string, 16);
        out.write(i);
    }
    return out.toByteArray();
}

From source file:Main.java

public static final String getXMLDecl(final byte[] data) {
    boolean foundTag = false;
    for (int i = 0; i < data.length && !foundTag; i++) {
        if (data[i] == '<') {
            foundTag = true;//w  w w . j a  v  a 2  s. co m

            /*
             * Need to gather the next 4 non-zero values and test them
             * because greater than 8-bytes character encodings will be
             * represented with two bits
             */
            boolean foundQuestionMark = false;
            int placeInDeclString = 0;
            final byte[] declString = new byte[4];
            int x = (i + 1);
            for (; x < data.length; x++) {

                if (data[x] == 0) {
                    continue;
                }

                if (!foundQuestionMark && data[x] != '?') {
                    break;
                } else {
                    foundQuestionMark = true;
                }

                declString[placeInDeclString] = data[x];
                placeInDeclString++;

                if (placeInDeclString >= 4) {
                    break;
                }
            }

            if (placeInDeclString == 4 && declString[0] == '?' && declString[1] == 'x' && declString[2] == 'm'
                    && declString[3] == 'l')

            {

                final ByteArrayOutputStream out = new ByteArrayOutputStream(150);

                out.write('<');
                out.write(declString, 0, 4);

                for (int j = (x + 1); j < data.length; j++) {
                    if (data[j] != 0) {
                        out.write(data[j]);
                    }

                    if (data[j] == '?') {
                        j++;
                        /*
                         * When we find this we have to start looking for the end tag
                         */
                        for (; j < data.length; j++) {
                            if (data[j] == 0) {
                                continue;
                            }

                            out.write(data[j]);

                            if (data[j] != '>') {
                                break;
                            }

                            return new String(out.toByteArray());
                        }

                    }
                }
            }
        }
    }
    return null;
}

From source file:Main.java

public static byte[] readByteArray(Context context, String fileName) throws IOException {

    byte[] data;/*from  w w w .  j av a2  s  .c om*/
    int c;

    FileInputStream fis = context.openFileInput(fileName);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    BufferedInputStream bos = new BufferedInputStream(fis);

    while ((c = bos.read()) != -1) {
        byteArrayOutputStream.write(c);
    }

    data = byteArrayOutputStream.toByteArray();

    bos.close();
    byteArrayOutputStream.close();
    fis.close();

    return data;
}

From source file:com.plexobject.testplayer.dao.jdbc.GenericDaoJdbc.java

protected static Object toObject(Blob blob) {
    try {//from  w  w  w. ja v a2  s .  co m
        if (blob == null)
            return null;
        InputStream in = blob.getBinaryStream();
        if (in == null)
            return null;
        int c;
        ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
        while ((c = in.read()) != -1) {
            out.write(c);
        }
        byte[] b = out.toByteArray();
        if (b.length == 0)
            return null;
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(b));
        Object object = ois.readObject();
        ois.close();
        return object;
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new DaoException("Failed to deserialize", e);
    }
}

From source file:at.jku.rdfstats.hist.builder.HistogramCodec.java

protected static void writeShort(ByteArrayOutputStream stream, short s) {
    stream.write((byte) (s >>> 8));
    stream.write((byte) s);
}

From source file:Main.java

public static void writeLength(ByteArrayOutputStream baos, int len) {
    if (len < 0) {
        throw new RuntimeException("Invalid length < 0");
    }/*w w  w .j a  v a2  s  .  c  o m*/
    if (len <= 127) {
        //MSB = 0
        baos.write(len);
    } else if (len <= 16383) {
        // MSB = 10
        int lowbyte = len % 64;
        int highbyte = len / 64;
        baos.write(lowbyte + 128);
        baos.write(highbyte);
    } else if (len <= 2097151) {
        // MSB = 110
        int lowbyte = len % 32;
        int midbyte = (len / 32) % 256;
        int highbyte = len / 32 / 256;
        baos.write(lowbyte + 128 + 64);
        baos.write(midbyte);
        baos.write(highbyte);
    } else {
        throw new RuntimeException("Invalid length > 2^21-1");
    }
}

From source file:de.kapsi.net.daap.DaapHeaderConstructor.java

/**
 * Converts statusLine and headers to an byte-Array
 *///from   ww  w. ja v a  2 s .  co m
private static byte[] toByteArray(String statusLine, Header[] headers)
        throws UnsupportedEncodingException, IOException {

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    out.write(statusLine.getBytes(ISO_8859_1));
    out.write(CRLF);

    for (int i = 0; i < headers.length; i++) {
        out.write(headers[i].toExternalForm().getBytes(ISO_8859_1));
    }

    out.write(CRLF);
    out.close();

    return out.toByteArray();
}

From source file:Main.java

public static byte[] hexDecode(String data) {
    if (null == data) {
        return null;
    }//from  w  ww  . ja v a 2 s .  c  om
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    for (int i = 0; i < data.length(); i += 2) {
        String onebyte = data.substring(i, i + 2);
        int b = Integer.parseInt(onebyte, 16) & 0xff;
        out.write(b);
    }
    return out.toByteArray();
}

From source file:Main.java

public static String file2String(File file) {
    FileInputStream fis = null;/*from ww  w  .j  a  va  2s  . c o  m*/
    ByteArrayOutputStream baos = null;
    try {
        fis = new FileInputStream(file);
        baos = new ByteArrayOutputStream();
        int i;
        while ((i = fis.read()) != -1) {
            baos.write(i);
        }
        String str = baos.toString();
        return str;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (fis != null)
                fis.close();
            if (baos != null)
                baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}