Example usage for java.io ByteArrayInputStream read

List of usage examples for java.io ByteArrayInputStream read

Introduction

In this page you can find the example usage for java.io ByteArrayInputStream read.

Prototype

public synchronized int read(byte b[], int off, int len) 

Source Link

Document

Reads up to len bytes of data into an array of bytes from this input stream.

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {

    byte[] buf = { 65, 66, 67, 68, 69 };

    ByteArrayInputStream bais = new ByteArrayInputStream(buf);

    byte[] b = new byte[4];
    int num = bais.read(b, 2, 2);

    System.out.println("Bytes read: " + num);

    for (byte s : b) {
        char c = (char) s;
        System.out.println(s);//from   ww w.j  a v a2  s.c  o m
        if (s == 0) {
            System.out.println(": Null");
        } else {
            System.out.println(": " + c);
        }
    }

}

From source file:Main.java

public static void main(String args[]) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));//from   w ww. jav a 2 s. c  o m
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());
    ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
    int inBytes = inStream.available();
    System.out.println("inStream has " + inBytes + " available bytes");
    byte inBuf[] = new byte[inBytes];
    int bytesRead = inStream.read(inBuf, 0, inBytes);
    System.out.println(bytesRead + " bytes were read");
    System.out.println("They are: " + new String(inBuf));
}

From source file:ByteArrayIOApp.java

public static void main(String args[]) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));//from  w  ww  . j av  a 2 s  .  c o m
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());
    ByteArrayInputStream inStream;
    inStream = new ByteArrayInputStream(outStream.toByteArray());
    int inBytes = inStream.available();
    System.out.println("inStream has " + inBytes + " available bytes");
    byte inBuf[] = new byte[inBytes];
    int bytesRead = inStream.read(inBuf, 0, inBytes);
    System.out.println(bytesRead + " bytes were read");
    System.out.println("They are: " + new String(inBuf));
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));//w w  w.j  a v a2s .co m
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());

    ByteArrayInputStream inStream;
    inStream = new ByteArrayInputStream(outStream.toByteArray());
    int inBytes = inStream.available();

    System.out.println("inStream has " + inBytes + " available bytes");
    byte inBuf[] = new byte[inBytes];
    int bytesRead = inStream.read(inBuf, 0, inBytes);

    System.out.println(bytesRead + " bytes were read");
    System.out.println("They are: " + new String(inBuf));
}

From source file:Main.java

public static byte[] readData(ByteArrayInputStream bais) {
    int len = getLength(bais);
    byte[] data = new byte[len];
    bais.read(data, 0, len);
    return data;//from  w  w  w. j  av  a 2s . c  o  m
}

From source file:Main.java

public static String toString(ByteArrayInputStream is) {
    int size = is.available();
    char[] theChars = new char[size];
    byte[] bytes = new byte[size];

    is.read(bytes, 0, size);
    for (int i = 0; i < size;)
        theChars[i] = (char) (bytes[i++] & 0xff);

    return new String(theChars);
}

From source file:com.jadarstudios.rankcapes.bukkit.CapePackValidator.java

/**
 * Utility method to detect if the bytes given are a zip file.
 *
 * @param bytes the bytes of the file/*w w w .j a  va 2 s  . c  o m*/
 *
 * @return if it is a zip file
 */
public static boolean isZipFile(byte[] bytes) {
    ByteArrayInputStream input = new ByteArrayInputStream(bytes);

    ByteBuffer buffer = ByteBuffer.allocate(4);
    input.read(buffer.array(), 0, buffer.capacity());

    short packIdentifier = buffer.getShort();

    return packIdentifier == ZIP_IDENTIFIER;
}

From source file:Main.java

public static long write(String fileName, String data, int position) throws FileNotFoundException, IOException {
    boolean append = false;
    if (position > 0) {
        truncateFile(fileName, position);
        append = true;/*from  ww  w.j  a  v  a  2  s  .c  o  m*/
    }

    byte[] rawData = data.getBytes();
    ByteArrayInputStream in = new ByteArrayInputStream(rawData);
    FileOutputStream out = new FileOutputStream(fileName, append);
    byte buff[] = new byte[rawData.length];
    in.read(buff, 0, buff.length);
    out.write(buff, 0, rawData.length);
    out.flush();
    out.close();

    return data.length();
}

From source file:cn.sharesdk.analysis.net.NetworkHelper.java

public static String Base64Gzip(String str) {
    ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    String result = null;//w  w w.j av a 2s. co m
    // gzip
    GZIPOutputStream gos;
    try {
        gos = new GZIPOutputStream(baos);
        int count;
        byte data[] = new byte[1024];
        while ((count = bais.read(data, 0, 1024)) != -1) {
            gos.write(data, 0, count);
        }
        gos.finish();
        gos.close();

        byte[] output = baos.toByteArray();
        baos.flush();
        baos.close();
        bais.close();

        result = Base64.encodeToString(output, Base64.NO_WRAP);

    } catch (IOException e) {
        e.printStackTrace();
        Ln.e("NetworkHelper", "Base64Gzip == >>", e);
    }

    //Ln.i("after base64gizp", result);
    return result;
}

From source file:cn.sharesdk.net.NetworkHelper.java

public static String Base64Gzip(String str) {
    ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    String result = null;//www .j  a  v  a  2  s . c  o m
    // gzip
    GZIPOutputStream gos;
    try {
        gos = new GZIPOutputStream(baos);
        int count;
        byte data[] = new byte[1024];
        while ((count = bais.read(data, 0, 1024)) != -1) {
            gos.write(data, 0, count);
        }
        gos.finish();
        gos.close();

        byte[] output = baos.toByteArray();
        baos.flush();
        baos.close();
        bais.close();

        result = Base64.encodeToString(output, Base64.NO_WRAP);
    } catch (IOException e) {
        e.printStackTrace();
        Ln.i("NetworkHelper", "Base64Gzip == >>", e);
    }
    return result;
}