Search in sources :

Example 51 with DataInputStream

use of java.io.DataInputStream in project guava by google.

the class LittleEndianDataOutputStreamTest method testWriteBytes_discardHighOrderBytes.

// testing a deprecated method
@SuppressWarnings("deprecation")
public void testWriteBytes_discardHighOrderBytes() throws IOException {
    /* Write out various test values in LITTLE ENDIAN FORMAT */
    out.writeBytes("ꪪꪻ꫌");
    byte[] data = baos.toByteArray();
    /* Setup input streams */
    DataInput in = new DataInputStream(new ByteArrayInputStream(data));
    /* Read in various values NORMALLY */
    byte[] b = new byte[3];
    in.readFully(b);
    byte[] expected = { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC };
    assertEquals(expected, b);
}
Also used : DataInput(java.io.DataInput) ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream)

Example 52 with DataInputStream

use of java.io.DataInputStream in project guava by google.

the class BloomFilter method readFrom.

/**
   * Reads a byte stream, which was written by {@linkplain #writeTo(OutputStream)}, into a
   * {@code BloomFilter<T>}.
   *
   * The {@code Funnel} to be used is not encoded in the stream, so it must be provided here.
   * <b>Warning:</b> the funnel provided <b>must</b> behave identically to the one used to populate
   * the original Bloom filter!
   *
   * @throws IOException if the InputStream throws an {@code IOException}, or if its data does not
   *     appear to be a BloomFilter serialized using the {@linkplain #writeTo(OutputStream)} method.
   */
public static <T> BloomFilter<T> readFrom(InputStream in, Funnel<? super T> funnel) throws IOException {
    checkNotNull(in, "InputStream");
    checkNotNull(funnel, "Funnel");
    int strategyOrdinal = -1;
    int numHashFunctions = -1;
    int dataLength = -1;
    try {
        DataInputStream din = new DataInputStream(in);
        // currently this assumes there is no negative ordinal; will have to be updated if we
        // add non-stateless strategies (for which we've reserved negative ordinals; see
        // Strategy.ordinal()).
        strategyOrdinal = din.readByte();
        numHashFunctions = UnsignedBytes.toInt(din.readByte());
        dataLength = din.readInt();
        Strategy strategy = BloomFilterStrategies.values()[strategyOrdinal];
        long[] data = new long[dataLength];
        for (int i = 0; i < data.length; i++) {
            data[i] = din.readLong();
        }
        return new BloomFilter<T>(new BitArray(data), numHashFunctions, funnel, strategy);
    } catch (RuntimeException e) {
        String message = "Unable to deserialize BloomFilter from InputStream." + " strategyOrdinal: " + strategyOrdinal + " numHashFunctions: " + numHashFunctions + " dataLength: " + dataLength;
        throw new IOException(message, e);
    }
}
Also used : BitArray(com.google.common.hash.BloomFilterStrategies.BitArray) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 53 with DataInputStream

use of java.io.DataInputStream in project j2objc by google.

the class OldDataInputStreamTest method test_read$BII.

public void test_read$BII() throws IOException {
    byte[] rbytes = new byte[testLength - 5];
    Support_ASimpleInputStream sis = new Support_ASimpleInputStream();
    int r;
    os.write(fileString.getBytes());
    os.close();
    openDataInputStream();
    r = dis.read(rbytes, 1, testLength - 10);
    assertEquals("Test 1: Incorrect number of bytes read;", testLength - 10, r);
    assertEquals("Test 2: Incorrect data read.", 0, rbytes[0]);
    assertTrue("Test 3: Incorrect data written or read.", new String(rbytes, 1, r).equals(fileString.substring(0, r)));
    r = dis.read(rbytes, 0, 15);
    assertEquals("Test 3: Incorrect number of bytes read;", 10, r);
    assertTrue("Test 4: Incorrect data written or read.", new String(rbytes, 0, r).equals(fileString.substring(testLength - 10)));
    dis.close();
    sis.throwExceptionOnNextUse = true;
    dis = new DataInputStream(sis);
    try {
        dis.read(rbytes, 1, 5);
        fail("Test 5: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
}
Also used : Support_ASimpleInputStream(tests.support.Support_ASimpleInputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 54 with DataInputStream

use of java.io.DataInputStream in project j2objc by google.

the class OldDataInputStreamTest method test_readFully$BII_Exception.

public void test_readFully$BII_Exception() throws IOException {
    DataInputStream is = new DataInputStream(new ByteArrayInputStream(new byte[testLength]));
    byte[] byteArray = new byte[testLength];
    try {
        is.readFully(byteArray, 0, -1);
        fail("Test 1: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected.
    }
    try {
        is.readFully(byteArray, 0, byteArray.length + 1);
        fail("Test 2: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected.
    }
    try {
        is.readFully(byteArray, 1, byteArray.length);
        fail("Test 3: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected.
    }
    try {
        is.readFully(byteArray, -1, byteArray.length);
        fail("Test 4: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected.
    }
    try {
        is.readFully(null, 0, 1);
        fail("Test 5: NullPointerException expected.");
    } catch (NullPointerException e) {
    // Expected.
    }
    is = new DataInputStream(null);
    try {
        is.readFully(byteArray, 0, 1);
        fail("Test 6: NullPointerException expected.");
    } catch (NullPointerException e) {
    // Expected.
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream)

Example 55 with DataInputStream

use of java.io.DataInputStream in project j2objc by google.

the class DerInputStream method readVector.

/*
     * Read a "vector" of values ... set or sequence have the
     * same encoding, except for the initial tag, so both use
     * this same helper routine.
     */
protected DerValue[] readVector(int startLen, boolean originalEncodedFormRetained) throws IOException {
    DerInputStream newstr;
    byte lenByte = (byte) buffer.read();
    int len = getLength((lenByte & 0xff), buffer);
    if (len == -1) {
        // indefinite length encoding found
        int readLen = buffer.available();
        // for tag and length bytes
        int offset = 2;
        byte[] indefData = new byte[readLen + offset];
        indefData[0] = tag;
        indefData[1] = lenByte;
        DataInputStream dis = new DataInputStream(buffer);
        dis.readFully(indefData, offset, readLen);
        dis.close();
        DerIndefLenConverter derIn = new DerIndefLenConverter();
        buffer = new DerInputBuffer(derIn.convert(indefData));
        if (tag != buffer.read())
            throw new IOException("Indefinite length encoding" + " not supported");
        len = DerInputStream.getLength(buffer);
    }
    if (len == 0)
        // used only for missing optionals
        return new DerValue[0];
    /*
         * Create a temporary stream from which to read the data,
         * unless it's not really needed.
         */
    if (buffer.available() == len)
        newstr = this;
    else
        newstr = subStream(len, true);
    /*
         * Pull values out of the stream.
         */
    Vector<DerValue> vec = new Vector<DerValue>(startLen);
    DerValue value;
    do {
        value = new DerValue(newstr.buffer, originalEncodedFormRetained);
        vec.addElement(value);
    } while (newstr.available() > 0);
    if (newstr.available() != 0)
        throw new IOException("extra data at end of vector");
    /*
         * Now stick them into the array we're returning.
         */
    int i, max = vec.size();
    DerValue[] retval = new DerValue[max];
    for (i = 0; i < max; i++) retval[i] = vec.elementAt(i);
    return retval;
}
Also used : IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) Vector(java.util.Vector)

Aggregations

DataInputStream (java.io.DataInputStream)1544 ByteArrayInputStream (java.io.ByteArrayInputStream)635 IOException (java.io.IOException)582 DataOutputStream (java.io.DataOutputStream)315 FileInputStream (java.io.FileInputStream)313 Test (org.junit.Test)274 ByteArrayOutputStream (java.io.ByteArrayOutputStream)198 BufferedInputStream (java.io.BufferedInputStream)157 File (java.io.File)151 DataInput (java.io.DataInput)112 InputStream (java.io.InputStream)109 EOFException (java.io.EOFException)90 ArrayList (java.util.ArrayList)90 FileNotFoundException (java.io.FileNotFoundException)88 FileOutputStream (java.io.FileOutputStream)52 InputStreamReader (java.io.InputStreamReader)52 BufferedReader (java.io.BufferedReader)50 Socket (java.net.Socket)44 ByteBuffer (java.nio.ByteBuffer)40 HashMap (java.util.HashMap)38