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

Source Link

Document

Reads the next byte of data from this input stream.

Usage

From source file:Main.java

public static String getHexStr(byte[] b) {
    StringBuilder builder = new StringBuilder();
    ByteArrayInputStream input = new ByteArrayInputStream(b);
    int i = 0;/*from www.j  ava  2s  .c  o m*/
    while ((i = input.read()) != -1) {
        builder.append(
                Integer.toHexString(i).length() == 2 ? Integer.toHexString(i) : 0 + Integer.toHexString(i));
        builder.append(" ");
    }
    return builder.toString();
}

From source file:Main.java

/**
 * @param b//  w  w w.j  a v a2  s .c  o  m
 * @return
 */
public static String getShi(byte[] buf) {
    StringBuilder builder = new StringBuilder();
    ByteArrayInputStream input = new ByteArrayInputStream(buf);
    int i = 0;
    while ((i = input.read()) != -1) {
        builder.append(i);
    }
    return builder.toString();
}

From source file:Main.java

/** 
 *Copies source stream to target./*from   ww w.j av a 2  s. co m*/
 * 
 *@param source The source. 
 *@param name target The target.
 **/
protected static void copyStream(ByteArrayOutputStream source, ByteArrayOutputStream target) throws Exception {
    // See if this is a MemoryStream -- we can use WriteTo.
    ByteArrayOutputStream memContentStream = source;
    if (memContentStream != null) {
        memContentStream.writeTo(target);
        memContentStream.flush();
    } else {
        // Otherwise, copy data through a buffer

        int c;
        ByteArrayInputStream inStream = new ByteArrayInputStream(source.toByteArray());

        while ((c = inStream.read()) != -1) {
            target.write((char) c);

        }
    }
}

From source file:Main.java

protected static byte[] parseWapTextString(ByteArrayInputStream bais) {
    assert (null != bais);

    bais.mark(1);/*from w w  w  .j  a va 2s  . c om*/

    int temp = bais.read();
    assert (-1 != temp);

    if (temp == QUOTE) {
        bais.mark(1);
    }

    return getWapTextString(bais);
}

From source file:Main.java

/**
 * read length value from EMV tag bytestream
 * /*from   w  ww  .j  a va 2 s.co m*/
 * source: https://code.google.com/p/javaemvreader/
 * 
 * @param stream
 * @return
 */
private static int readTagLength(ByteArrayInputStream stream) {
    // Find LENGTH bytes
    int length;
    int tmpLength = stream.read();

    if (tmpLength <= 127) { // 0111 1111
        // short length form
        length = tmpLength;
    } else if (tmpLength == 128) { // 1000 0000
        // length identifies indefinite form, will be set later
        length = tmpLength;
    } else {
        // long length form
        int numberOfLengthOctets = tmpLength & 127; // turn off 8th bit
        tmpLength = 0;
        for (int i = 0; i < numberOfLengthOctets; i++) {
            int nextLengthOctet = stream.read();
            tmpLength <<= 8;
            tmpLength |= nextLengthOctet;
        }
        length = tmpLength;
    }
    return length;
}

From source file:Main.java

public static String readStringSmart(ByteArrayInputStream bais, List<String> knownStrings) {
    switch (bais.read()) {
    case STRING_NULL: {
        return null;
    }//from w  w w  .j  ava  2s.  com
    case STRING_FROM_LIST: {
        int index = getLength(bais);
        return knownStrings.get(index);
    }
    case STRING_PREFIX_FROM_LIST: {
        int index = getLength(bais);
        String prefix = knownStrings.get(index);
        return prefix + readString(bais);
    }
    case STRING_PREFIX_FROM_LIST_COMPRESSED: {
        int index = getLength(bais);
        String prefix = knownStrings.get(index);
        return prefix + readStringCompressed(bais);
    }
    case STRING_COMPRESSED: {
        return readStringCompressed(bais);
    }
    case STRING_FROM_BYTES: {
        return readString(bais);
    }
    default: {
        throw new RuntimeException("Cannot read smart string");
    }
    }
}

From source file:Main.java

protected static byte[] getWapTextString(ByteArrayInputStream bais) {
    assert (null != bais);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    int temp = bais.read();
    assert (-1 != temp);

    while ((-1 != temp) && ('\0' != temp)) {
        if (isText(temp)) {
            bos.write(temp);// w  w  w .j av  a 2  s.  c om
        }
        temp = bais.read();
        assert (-1 != temp);
    }

    if (bos.size() > 0) {
        return bos.toByteArray();
    }

    return null;
}

From source file:org.game.cs.core.condenser.steam.packets.SteamPacketFactory.java

/**
 * Reassembles the data of a split and/or compressed packet into a single
 * packet object/*from w w w.  j  av  a2 s  . com*/
 *
 * @param splitPackets An array of packet data
 * @param isCompressed whether the data of this packet is compressed
 * @param uncompressedSize The size of the decompressed packet data
 * @param packetChecksum The CRC32 checksum of the decompressed
 *        packet data
 * @throws SteamCondenserException if decompressing the packet data fails
 * @throws PacketFormatException if the calculated CRC32 checksum does not
 *         match the expected value
 * @return SteamPacket The reassembled packet
 * @see SteamPacketFactory#getPacketFromData
 */
public static SteamPacket reassemblePacket(ArrayList<byte[]> splitPackets, boolean isCompressed,
        int uncompressedSize, int packetChecksum) throws SteamCondenserException {
    byte[] packetData, tmpData;
    packetData = new byte[0];

    for (byte[] splitPacket : splitPackets) {
        tmpData = packetData;
        packetData = new byte[tmpData.length + splitPacket.length];
        System.arraycopy(tmpData, 0, packetData, 0, tmpData.length);
        System.arraycopy(splitPacket, 0, packetData, tmpData.length, splitPacket.length);
    }

    if (isCompressed) {
        try {
            ByteArrayInputStream stream = new ByteArrayInputStream(packetData);
            stream.read();
            stream.read();
            BZip2CompressorInputStream bzip2 = new BZip2CompressorInputStream(stream);
            byte[] uncompressedPacketData = new byte[uncompressedSize];
            bzip2.read(uncompressedPacketData, 0, uncompressedSize);

            CRC32 crc32 = new CRC32();
            crc32.update(uncompressedPacketData);
            int crc32checksum = (int) crc32.getValue();

            if (crc32checksum != packetChecksum) {
                throw new PacketFormatException("CRC32 checksum mismatch of uncompressed packet data.");
            }
            packetData = uncompressedPacketData;
        } catch (IOException e) {
            throw new SteamCondenserException(e.getMessage(), e);
        }
    }

    packetData = new String(packetData).substring(4).getBytes();

    return SteamPacketFactory.getPacketFromData(packetData);
}

From source file:com.github.koraktor.steamcondenser.servers.packets.SteamPacketFactory.java

/**
 * Reassembles the data of a split and/or compressed packet into a single
 * packet object//from  ww  w.  j av a  2  s  .  c  om
 *
 * @param splitPackets An array of packet data
 * @param isCompressed whether the data of this packet is compressed
 * @param uncompressedSize The size of the decompressed packet data
 * @param packetChecksum The CRC32 checksum of the decompressed
 *        packet data
 * @throws SteamCondenserException if decompressing the packet data fails
 * @throws PacketFormatException if the calculated CRC32 checksum does not
 *         match the expected value
 * @return SteamPacket The reassembled packet
 * @see SteamPacketFactory#getPacketFromData
 */
public static SteamPacket reassemblePacket(ArrayList<byte[]> splitPackets, boolean isCompressed,
        int uncompressedSize, int packetChecksum) throws SteamCondenserException {
    byte[] packetData, tmpData;
    packetData = new byte[0];

    for (byte[] splitPacket : splitPackets) {
        tmpData = packetData;
        packetData = new byte[tmpData.length + splitPacket.length];
        System.arraycopy(tmpData, 0, packetData, 0, tmpData.length);
        System.arraycopy(splitPacket, 0, packetData, tmpData.length, splitPacket.length);
    }

    if (isCompressed) {
        try {
            ByteArrayInputStream stream = new ByteArrayInputStream(packetData);
            stream.read();
            stream.read();
            BZip2CompressorInputStream bzip2 = new BZip2CompressorInputStream(stream);
            byte[] uncompressedPacketData = new byte[uncompressedSize];
            bzip2.read(uncompressedPacketData, 0, uncompressedSize);

            CRC32 crc32 = new CRC32();
            crc32.update(uncompressedPacketData);
            int crc32checksum = (int) crc32.getValue();

            if (crc32checksum != packetChecksum) {
                throw new PacketFormatException("CRC32 checksum mismatch of uncompressed packet data.");
            }
            packetData = uncompressedPacketData;
        } catch (IOException e) {
            throw new SteamCondenserException(e.getMessage(), e);
        }
    }

    tmpData = packetData;
    packetData = new byte[tmpData.length - 4];
    System.arraycopy(tmpData, 4, packetData, 0, tmpData.length - 4);

    return SteamPacketFactory.getPacketFromData(packetData);
}

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

protected static short readShort(ByteArrayInputStream stream) {
    short s = 0;/*  www  .  j a  v a2 s.co m*/
    int next = stream.read();
    if (next >= 0)
        s |= next << 8;
    next = stream.read();
    if (next >= 0)
        s |= stream.read();

    return s;
}