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

public static Image getImage(Class relativeClass, String filename) {
    Image returnValue = null;// w w  w . j a  va 2 s .c o  m
    InputStream is = relativeClass.getResourceAsStream(filename);
    if (is != null) {
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            int ch;
            while ((ch = bis.read()) != -1) {
                baos.write(ch);
            }
            returnValue = Toolkit.getDefaultToolkit().createImage(baos.toByteArray());
        } catch (IOException exception) {
            System.err.println("Error loading: " + filename);
        }
    }
    return returnValue;
}

From source file:Main.java

public static byte[] inputStreamToByte(InputStream is) {
    try {/*w  w  w . ja va2s  . c o  m*/
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
        int ch;
        while ((ch = is.read()) != -1) {
            bytestream.write(ch);
        }
        byte imgdata[] = bytestream.toByteArray();
        bytestream.close();
        return imgdata;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:edu.ku.brc.specify.plugins.ipadexporter.MD5Checksum.java

/**
 * @param file/* w w  w . j av a  2 s  . co m*/
 * @return
 * @throws Exception
 */
public static String getMD5Checksum(final File file) throws Exception {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.reset();
    InputStream fis = new FileInputStream(file);
    try {
        //ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fis);
        DigestInputStream digestInputStream = new DigestInputStream(fis, md);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int ch;
        while ((ch = digestInputStream.read()) >= 0) {
            byteArrayOutputStream.write(ch);
        }

        byte[] newInput = byteArrayOutputStream.toByteArray();
        return org.apache.commons.codec.digest.DigestUtils.md5Hex(newInput);
    } finally {
        fis.close();
    }
}

From source file:Main.java

public static byte[] inputStreamToByte(InputStream is) {
    try {//from   w w  w . jav  a 2 s . com
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
        int ch;
        while ((ch = is.read()) != -1) {
            bytestream.write(ch);
        }
        byte imgdata[] = bytestream.toByteArray();
        bytestream.close();
        return imgdata;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static Image getImage(Class relativeClass, String filename) {
    Image returnValue = null;//from   www .j  av  a  2 s.  co m
    InputStream is = relativeClass.getResourceAsStream(filename);
    if (is != null) {
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            int ch;
            while ((ch = bis.read()) != -1) {
                baos.write(ch);
            }
            Toolkit t = Toolkit.getDefaultToolkit();

            returnValue = t.createImage(baos.toByteArray());
        } catch (IOException exception) {
            System.err.println("Error loading: " + filename);
        }
    }
    return returnValue;
}

From source file:Main.java

private static byte[] mergeBytes(byte[] in, ConcurrentLinkedQueue<byte[]> decompressUnfinishedDataQueue) {

    if (!decompressUnfinishedDataQueue.isEmpty()) {

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {/* ww w  .j a  v a 2  s.  c o  m*/
            while (!decompressUnfinishedDataQueue.isEmpty()) {
                out.write(decompressUnfinishedDataQueue.poll());
            }
            out.write(in);
            in = out.toByteArray();

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                out.close();
            } catch (IOException e) {
            }
        }
    }
    return in;
}

From source file:Main.java

public static String readString(byte[] message, int start) {
    int pos = start;
    final int len = message.length;

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    while (pos < len) {
        if (message[pos] == '\0') {
            ++pos;//w w  w .j  ava 2 s  .co  m
            break;
        }
        os.write(message[pos++]);
    }

    return os.toString();
}

From source file:Main.java

public static byte[] toByteArray(InputStream is) {
    byte[] ret = null;
    try {//from  www .jav a2  s.c  o  m
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int ch;
        while ((ch = is.read()) != -1) {
            byteArrayOutputStream.write(ch);
        }
        ret = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.close();
    } catch (Exception e) {
    } finally {
        try {
            is.close();
        } catch (IOException e) {
        }
    }
    return ret;
}

From source file:dk.netarkivet.common.utils.InputStreamUtils.java

/**
 * Reads a raw line from an InputStream, up till \n. Since HTTP allows \r\n and \n as terminators, this gets the
 * whole line. This code is adapted from org.apache.commons.httpclient.HttpParser
 *
 * @param inputStream A stream to read from.
 * @return Array of bytes read or null if none are available.
 * @throws IOException if the underlying reads fail
 *///from w  ww.  ja v a2s  .  com
public static byte[] readRawLine(InputStream inputStream) throws IOException {
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    int ch;
    while ((ch = inputStream.read()) >= 0) {
        buf.write(ch);
        if (ch == '\n') {
            break;
        }
    }
    if (buf.size() == 0) {
        return null;
    }
    return buf.toByteArray();
}

From source file:Main.java

public static String readRawTextFile(Context ctx, int resId) {
    InputStream inputStream = ctx.getResources().openRawResource(resId);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int i;/*from w ww. jav a  2s . co m*/
    try {
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        return null;
    }
    return byteArrayOutputStream.toString();
}