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;
}
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);
}
}
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());
}
}
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());
}
}
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();
}
}
}
Aggregations