Search in sources :

Example 61 with DataInputStream

use of java.io.DataInputStream in project Cloud9 by lintool.

the class HadoopAlign method loadVocab.

public static Vocab loadVocab(Path path, FileSystem fileSys) throws IOException {
    DataInput in = new DataInputStream(new BufferedInputStream(fileSys.open(path)));
    VocabularyWritable at = new VocabularyWritable();
    at.readFields(in);
    return at;
}
Also used : DataInput(java.io.DataInput) BufferedInputStream(java.io.BufferedInputStream) VocabularyWritable(edu.umd.hooka.VocabularyWritable) DataInputStream(java.io.DataInputStream)

Example 62 with DataInputStream

use of java.io.DataInputStream in project Cloud9 by lintool.

the class CLI_Int2Words method main.

/**
	 * @param args
	 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    if (args.length != 1) {
        System.err.println("Usage: CLI_Int2Words <vocfile.dat>");
        System.exit(1);
    }
    try {
        Path pve = new Path(args[0]);
        org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
        FileSystem fileSys = FileSystem.get(conf);
        DataInputStream dis = new DataInputStream(new BufferedInputStream(fileSys.open(pve)));
        VocabularyWritable v = new VocabularyWritable();
        v.readFields(dis);
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        String l = null;
        while ((l = r.readLine()) != null) {
            String[] nums = l.split("\\s+");
            //System.err.println("words: " + nums.length);
            for (String n : nums) {
                if (n.length() == 0)
                    continue;
                System.out.print(v.get(Integer.parseInt(n)));
                System.out.print(' ');
            }
            System.out.println();
        }
    } catch (IOException e) {
        System.err.println("Caught: " + e);
        System.exit(1);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) FileSystem(org.apache.hadoop.fs.FileSystem) BufferedReader(java.io.BufferedReader)

Example 63 with DataInputStream

use of java.io.DataInputStream in project flink by apache.

the class MemorySegmentTestBase method testDataInputOutputStreamUnderflowOverflow.

@Test
public void testDataInputOutputStreamUnderflowOverflow() {
    try {
        final int segmentSize = 1337;
        // segment with random contents
        MemorySegment seg = createSegment(segmentSize);
        byte[] bytes = new byte[segmentSize];
        random.nextBytes(bytes);
        seg.put(0, bytes);
        // a stream that we cannot fully write to
        DataOutputStream out = new DataOutputStream(new OutputStream() {

            int bytesSoFar = 0;

            @Override
            public void write(int b) throws IOException {
                bytesSoFar++;
                if (bytesSoFar > segmentSize / 2) {
                    throw new IOException("overflow");
                }
            }
        });
        // write the segment in chunks into the stream
        try {
            int pos = 0;
            while (pos < pageSize) {
                int len = random.nextInt(segmentSize / 10);
                len = Math.min(len, pageSize - pos);
                seg.get(out, pos, len);
                pos += len;
            }
            fail("Should fail with an IOException");
        } catch (IOException e) {
        // expected
        }
        DataInputStream in = new DataInputStream(new ByteArrayInputStream(new byte[segmentSize / 2]));
        try {
            int pos = 0;
            while (pos < pageSize) {
                int len = random.nextInt(segmentSize / 10);
                len = Math.min(len, pageSize - pos);
                seg.put(in, pos, len);
                pos += len;
            }
            fail("Should fail with an EOFException");
        } catch (EOFException e) {
        // expected
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataOutputStream(java.io.DataOutputStream) EOFException(java.io.EOFException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) BufferOverflowException(java.nio.BufferOverflowException) IOException(java.io.IOException) EOFException(java.io.EOFException) BufferUnderflowException(java.nio.BufferUnderflowException) Test(org.junit.Test)

Example 64 with DataInputStream

use of java.io.DataInputStream in project flink by apache.

the class MemorySegmentTestBase method testDataInputOutputOutOfBounds.

@Test
public void testDataInputOutputOutOfBounds() {
    try {
        final int segmentSize = 52;
        // segment with random contents
        MemorySegment seg = createSegment(segmentSize);
        byte[] bytes = new byte[segmentSize];
        random.nextBytes(bytes);
        seg.put(0, bytes);
        // out of bounds when writing
        {
            DataOutputStream out = new DataOutputStream(new ByteArrayOutputStream());
            try {
                seg.get(out, -1, segmentSize / 2);
                fail("IndexOutOfBoundsException expected");
            } catch (Exception e) {
                assertTrue(e instanceof IndexOutOfBoundsException);
            }
            try {
                seg.get(out, segmentSize, segmentSize / 2);
                fail("IndexOutOfBoundsException expected");
            } catch (Exception e) {
                assertTrue(e instanceof IndexOutOfBoundsException);
            }
            try {
                seg.get(out, -segmentSize, segmentSize / 2);
                fail("IndexOutOfBoundsException expected");
            } catch (Exception e) {
                assertTrue(e instanceof IndexOutOfBoundsException);
            }
            try {
                seg.get(out, Integer.MIN_VALUE, segmentSize / 2);
                fail("IndexOutOfBoundsException expected");
            } catch (Exception e) {
                assertTrue(e instanceof IndexOutOfBoundsException);
            }
            try {
                seg.get(out, Integer.MAX_VALUE, segmentSize / 2);
                fail("IndexOutOfBoundsException expected");
            } catch (Exception e) {
                assertTrue(e instanceof IndexOutOfBoundsException);
            }
        }
        // out of bounds when reading
        {
            DataInputStream in = new DataInputStream(new ByteArrayInputStream(new byte[segmentSize]));
            try {
                seg.put(in, -1, segmentSize / 2);
                fail("IndexOutOfBoundsException expected");
            } catch (Exception e) {
                assertTrue(e instanceof IndexOutOfBoundsException);
            }
            try {
                seg.put(in, segmentSize, segmentSize / 2);
                fail("IndexOutOfBoundsException expected");
            } catch (Exception e) {
                assertTrue(e instanceof IndexOutOfBoundsException);
            }
            try {
                seg.put(in, -segmentSize, segmentSize / 2);
                fail("IndexOutOfBoundsException expected");
            } catch (Exception e) {
                assertTrue(e instanceof IndexOutOfBoundsException);
            }
            try {
                seg.put(in, Integer.MIN_VALUE, segmentSize / 2);
                fail("IndexOutOfBoundsException expected");
            } catch (Exception e) {
                assertTrue(e instanceof IndexOutOfBoundsException);
            }
            try {
                seg.put(in, Integer.MAX_VALUE, segmentSize / 2);
                fail("IndexOutOfBoundsException expected");
            } catch (Exception e) {
                assertTrue(e instanceof IndexOutOfBoundsException);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) BufferOverflowException(java.nio.BufferOverflowException) IOException(java.io.IOException) EOFException(java.io.EOFException) BufferUnderflowException(java.nio.BufferUnderflowException) Test(org.junit.Test)

Example 65 with DataInputStream

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

the class GwtBinaryTest method create.

@Override
public void create() {
    FileHandle handle = Gdx.files.internal("data/arial.ttf");
    bytes = new byte[(int) handle.length()];
    DataInputStream in = new DataInputStream(handle.read());
    for (int i = 0; i < 100; i++) {
        try {
            bytes[i] = in.readByte();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) 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