Search in sources :

Example 86 with DataInputStream

use of java.io.DataInputStream in project XobotOS by xamarin.

the class BluetoothService method readIncomingConnectionState.

private void readIncomingConnectionState() {
    synchronized (mIncomingConnections) {
        FileInputStream fstream = null;
        try {
            fstream = new FileInputStream(INCOMING_CONNECTION_FILE);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader file = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = file.readLine()) != null) {
                line = line.trim();
                if (line.length() == 0)
                    continue;
                String[] value = line.split(",");
                if (value != null && value.length == 3) {
                    Integer val1 = Integer.parseInt(value[1]);
                    Pair<Integer, String> val = new Pair(val1, value[2]);
                    mIncomingConnections.put(value[0], val);
                }
            }
        } catch (FileNotFoundException e) {
            log("FileNotFoundException: readIncomingConnectionState" + e.toString());
        } catch (IOException e) {
            log("IOException: readIncomingConnectionState" + e.toString());
        } finally {
            if (fstream != null) {
                try {
                    fstream.close();
                } catch (IOException e) {
                // Ignore
                }
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) Pair(android.util.Pair)

Example 87 with DataInputStream

use of java.io.DataInputStream in project hazelcast by hazelcast.

the class IOUtilTest method testNewInputStream_shouldThrowWhenByteBufferExhaustedAndTryingToReadFully.

@Test(expected = EOFException.class)
public void testNewInputStream_shouldThrowWhenByteBufferExhaustedAndTryingToReadFully() throws Exception {
    ByteBuffer buffer = ByteBuffer.wrap(new byte[SIZE]);
    DataInputStream inputStream = new DataInputStream(newInputStream(buffer));
    inputStream.readFully(new byte[SIZE]);
    inputStream.readFully(NON_EMPTY_BYTE_ARRAY);
}
Also used : SerializationUtil.createObjectDataInputStream(com.hazelcast.internal.serialization.impl.SerializationUtil.createObjectDataInputStream) DataInputStream(java.io.DataInputStream) ByteBuffer(java.nio.ByteBuffer) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 88 with DataInputStream

use of java.io.DataInputStream in project hazelcast by hazelcast.

the class IOUtilTest method testNewInputStream_shouldThrowWhenTryingToReadFullyFromEmptyByteBuffer.

@Test(expected = EOFException.class)
public void testNewInputStream_shouldThrowWhenTryingToReadFullyFromEmptyByteBuffer() throws Exception {
    ByteBuffer empty = ByteBuffer.wrap(EMPTY_BYTE_ARRAY);
    DataInputStream inputStream = new DataInputStream(newInputStream(empty));
    inputStream.readFully(NON_EMPTY_BYTE_ARRAY);
}
Also used : SerializationUtil.createObjectDataInputStream(com.hazelcast.internal.serialization.impl.SerializationUtil.createObjectDataInputStream) DataInputStream(java.io.DataInputStream) ByteBuffer(java.nio.ByteBuffer) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 89 with DataInputStream

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

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 90 with DataInputStream

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

the class LittleEndianDataOutputStreamTest method testWriteLittleEndian.

public void testWriteLittleEndian() throws IOException {
    /* Write out various test values in LITTLE ENDIAN FORMAT */
    out.write(new byte[] { -100, 100 });
    out.writeBoolean(true);
    out.writeBoolean(false);
    out.writeByte(100);
    out.writeByte(-100);
    out.writeByte((byte) 200);
    out.writeChar('a');
    out.writeShort((short) -30000);
    out.writeShort((short) 50000);
    out.writeInt(0xCAFEBABE);
    out.writeLong(0xDEADBEEFCAFEBABEL);
    out.writeUTF("Herby Derby");
    out.writeFloat(Float.intBitsToFloat(0xCAFEBABE));
    out.writeDouble(Double.longBitsToDouble(0xDEADBEEFCAFEBABEL));
    byte[] data = baos.toByteArray();
    /* Setup input streams */
    DataInput in = new DataInputStream(new ByteArrayInputStream(data));
    /* Read in various values NORMALLY */
    byte[] b = new byte[2];
    in.readFully(b);
    assertEquals(-100, b[0]);
    assertEquals(100, b[1]);
    assertEquals(true, in.readBoolean());
    assertEquals(false, in.readBoolean());
    assertEquals(100, in.readByte());
    assertEquals(-100, in.readByte());
    assertEquals(200, in.readUnsignedByte());
    assertEquals('愀', in.readChar());
    assertEquals(-12150, in.readShort());
    assertEquals(20675, in.readUnsignedShort());
    assertEquals(0xBEBAFECA, in.readInt());
    assertEquals(0xBEBAFECAEFBEADDEL, in.readLong());
    assertEquals("Herby Derby", in.readUTF());
    assertEquals(0xBEBAFECA, Float.floatToIntBits(in.readFloat()));
    assertEquals(0xBEBAFECAEFBEADDEL, Double.doubleToLongBits(in.readDouble()));
}
Also used : DataInput(java.io.DataInput) ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream)

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