Search in sources :

Example 46 with DataInputStream

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

the class ObjectDataInputStreamNonFinalMethodsTest method before.

@Before
public void before() throws Exception {
    byteOrder = BIG_ENDIAN;
    mockSerializationService = mock(InternalSerializationService.class);
    when(mockSerializationService.getByteOrder()).thenReturn(byteOrder);
    inputStream = new ByteArrayInputStream(INIT_DATA);
    in = new ObjectDataInputStream(inputStream, mockSerializationService);
    Field field = ObjectDataInputStream.class.getDeclaredField("dataInput");
    field.setAccessible(true);
    DataInputStream dataInput = (DataInputStream) field.get(in);
    dataInputSpy = spy(dataInput);
    field.set(in, dataInputSpy);
}
Also used : Field(java.lang.reflect.Field) ByteArrayInputStream(java.io.ByteArrayInputStream) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) DataInputStream(java.io.DataInputStream) Before(org.junit.Before)

Example 47 with DataInputStream

use of java.io.DataInputStream in project hibernate-orm by hibernate.

the class ClassFileArchiveEntryHandler method toClassFile.

private ClassFile toClassFile(ArchiveEntry entry) {
    final InputStream inputStream = entry.getStreamAccess().accessInputStream();
    final DataInputStream dataInputStream = new DataInputStream(inputStream);
    try {
        return new ClassFile(dataInputStream);
    } catch (IOException e) {
        throw new ArchiveException("Could not build ClassFile", e);
    } finally {
        try {
            dataInputStream.close();
        } catch (Exception ignore) {
        }
        try {
            inputStream.close();
        } catch (IOException ignore) {
        }
    }
}
Also used : ClassFile(javassist.bytecode.ClassFile) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ArchiveException(org.hibernate.boot.archive.spi.ArchiveException) ArchiveException(org.hibernate.boot.archive.spi.ArchiveException) IOException(java.io.IOException)

Example 48 with DataInputStream

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

the class ZipFile method getInputStream.

/**
     * Returns an input stream on the data of the specified {@code ZipEntry}.
     *
     * @param entry
     *            the ZipEntry.
     * @return an input stream of the data contained in the {@code ZipEntry}.
     * @throws IOException
     *             if an {@code IOException} occurs.
     * @throws IllegalStateException if this zip file has been closed.
     */
public InputStream getInputStream(ZipEntry entry) throws IOException {
    // Make sure this ZipEntry is in this Zip file.  We run it through the name lookup.
    entry = getEntry(entry.getName());
    if (entry == null) {
        return null;
    }
    // Create an InputStream at the right part of the file.
    RandomAccessFile localRaf = raf;
    synchronized (localRaf) {
        // We don't know the entry data's start position. All we have is the
        // position of the entry's local header.
        // http://www.pkware.com/documents/casestudies/APPNOTE.TXT
        RAFStream rafStream = new RAFStream(localRaf, entry.localHeaderRelOffset);
        DataInputStream is = new DataInputStream(rafStream);
        final int localMagic = Integer.reverseBytes(is.readInt());
        if (localMagic != LOCSIG) {
            throwZipException("Local File Header", localMagic);
        }
        is.skipBytes(2);
        // At position 6 we find the General Purpose Bit Flag.
        int gpbf = Short.reverseBytes(is.readShort()) & 0xffff;
        if ((gpbf & ZipFile.GPBF_UNSUPPORTED_MASK) != 0) {
            throw new ZipException("Invalid General Purpose Bit Flag: " + gpbf);
        }
        // Offset 26 has the file name length, and offset 28 has the extra field length.
        // These lengths can differ from the ones in the central header.
        is.skipBytes(18);
        int fileNameLength = Short.reverseBytes(is.readShort()) & 0xffff;
        int extraFieldLength = Short.reverseBytes(is.readShort()) & 0xffff;
        is.close();
        // Skip the variable-size file name and extra field data.
        rafStream.skip(fileNameLength + extraFieldLength);
        if (entry.compressionMethod == ZipEntry.STORED) {
            rafStream.endOffset = rafStream.offset + entry.size;
            return rafStream;
        } else {
            rafStream.endOffset = rafStream.offset + entry.compressedSize;
            int bufSize = Math.max(1024, (int) Math.min(entry.getSize(), 65535L));
            return new ZipInflaterInputStream(rafStream, new Inflater(true), bufSize, entry);
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) DataInputStream(java.io.DataInputStream)

Example 49 with DataInputStream

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

the class OldAndroidDataInputStreamTest method testDataInputStream.

public void testDataInputStream() throws Exception {
    String str = "AbCdEfGhIjKlM\nOpQ\rStUvWxYz";
    ByteArrayInputStream aa = new ByteArrayInputStream(str.getBytes());
    ByteArrayInputStream ba = new ByteArrayInputStream(str.getBytes());
    ByteArrayInputStream ca = new ByteArrayInputStream(str.getBytes());
    ByteArrayInputStream da = new ByteArrayInputStream(str.getBytes());
    DataInputStream a = new DataInputStream(aa);
    try {
        Assert.assertEquals(str, read(a));
    } finally {
        a.close();
    }
    DataInputStream b = new DataInputStream(ba);
    try {
        Assert.assertEquals("AbCdEfGhIj", read(b, 10));
    } finally {
        b.close();
    }
    DataInputStream c = new DataInputStream(ca);
    try {
        Assert.assertEquals("bdfhjl\np\rtvxz", skipRead(c));
    } finally {
        c.close();
    }
    DataInputStream d = new DataInputStream(da);
    try {
        assertEquals("AbCdEfGhIjKlM", d.readLine());
        assertEquals("OpQ", d.readLine());
        assertEquals("StUvWxYz", d.readLine());
    } finally {
        d.close();
    }
    ByteArrayOutputStream e = new ByteArrayOutputStream();
    DataOutputStream f = new DataOutputStream(e);
    try {
        f.writeBoolean(true);
        f.writeByte('a');
        f.writeBytes("BCD");
        f.writeChar('e');
        f.writeChars("FGH");
        f.writeUTF("ijklm");
        f.writeDouble(1);
        f.writeFloat(2);
        f.writeInt(3);
        f.writeLong(4);
        f.writeShort(5);
    } finally {
        f.close();
    }
    ByteArrayInputStream ga = new ByteArrayInputStream(e.toByteArray());
    DataInputStream g = new DataInputStream(ga);
    try {
        assertTrue(g.readBoolean());
        assertEquals('a', g.readByte());
        assertEquals(2, g.skipBytes(2));
        assertEquals('D', g.readByte());
        assertEquals('e', g.readChar());
        assertEquals('F', g.readChar());
        assertEquals('G', g.readChar());
        assertEquals('H', g.readChar());
        assertEquals("ijklm", g.readUTF());
        assertEquals(1, g.readDouble(), 0);
        assertEquals(2f, g.readFloat(), 0f);
        assertEquals(3, g.readInt());
        assertEquals(4, g.readLong());
        assertEquals(5, g.readShort());
    } finally {
        g.close();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream)

Example 50 with DataInputStream

use of java.io.DataInputStream in project google-authenticator by google.

the class PasscodeGenerator method hashToInt.

/**
   * Grabs a positive integer value from the input array starting at
   * the given offset.
   * @param bytes the array of bytes
   * @param start the index into the array to start grabbing bytes
   * @return the integer constructed from the four bytes in the array
   */
private int hashToInt(byte[] bytes, int start) {
    DataInput input = new DataInputStream(new ByteArrayInputStream(bytes, start, bytes.length - start));
    int val;
    try {
        val = input.readInt();
    } catch (IOException e) {
        throw new IllegalStateException(String.valueOf(e));
    }
    return val;
}
Also used : DataInput(java.io.DataInput) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) 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