List of usage examples for java.io DataInput readFully
void readFully(byte b[]) throws IOException;
From source file:cn.iie.haiep.hbase.value.Bytes.java
/** * Reads a fixed-size field and interprets it as a string padded with zeros. *///from www. j a va 2s. com public static String readStringFixedSize(final DataInput in, int size) throws IOException { byte[] b = new byte[size]; in.readFully(b); int n = b.length; while (n > 0 && b[n - 1] == 0) --n; return toString(b, 0, n); }
From source file:clueweb09.WarcRecord.java
public void readFields(DataInput in) throws IOException { warcHeader.readFields(in);/*w ww. j a v a 2 s. c o m*/ int contentLengthBytes = warcHeader.contentLength; warcContent = new byte[contentLengthBytes]; in.readFully(warcContent); }
From source file:com.fiorano.openesb.application.DmiObject.java
/** * This method reads the specified input stream and returns it as a <code>String</code> * object./*from w ww . j ava 2s . co m*/ * * @param is input stream * @return String * @exception IOException IOException */ protected String readUTF(DataInput is) throws IOException { int length = is.readInt(); if (length == -1) return null; byte[] buff = new byte[length]; is.readFully(buff); return new String(buff, "UTF-8"); }
From source file:bobs.is.compress.sevenzip.SevenZFile.java
private void readArchiveProperties(final DataInput input) throws IOException { // FIXME: the reference implementation just throws them away? int nid = input.readUnsignedByte(); while (nid != NID.kEnd) { final long propertySize = readUint64(input); final byte[] property = new byte[(int) propertySize]; input.readFully(property); nid = input.readUnsignedByte();/*from w ww. j a v a 2 s . c om*/ } }
From source file:bobs.is.compress.sevenzip.SevenZFile.java
private Folder readFolder(final DataInput header) throws IOException { final Folder folder = new Folder(); final long numCoders = readUint64(header); final Coder[] coders = new Coder[(int) numCoders]; long totalInStreams = 0; long totalOutStreams = 0; for (int i = 0; i < coders.length; i++) { coders[i] = new Coder(); final int bits = header.readUnsignedByte(); final int idSize = bits & 0xf; final boolean isSimple = (bits & 0x10) == 0; final boolean hasAttributes = (bits & 0x20) != 0; final boolean moreAlternativeMethods = (bits & 0x80) != 0; coders[i].decompressionMethodId = new byte[idSize]; header.readFully(coders[i].decompressionMethodId); if (isSimple) { coders[i].numInStreams = 1;/* w w w. j av a 2 s .c om*/ coders[i].numOutStreams = 1; } else { coders[i].numInStreams = readUint64(header); coders[i].numOutStreams = readUint64(header); } totalInStreams += coders[i].numInStreams; totalOutStreams += coders[i].numOutStreams; if (hasAttributes) { final long propertiesSize = readUint64(header); coders[i].properties = new byte[(int) propertiesSize]; header.readFully(coders[i].properties); } // would need to keep looping as above: while (moreAlternativeMethods) { throw new IOException("Alternative methods are unsupported, please report. " + "The reference implementation doesn't support them either."); } } folder.coders = coders; folder.totalInputStreams = totalInStreams; folder.totalOutputStreams = totalOutStreams; if (totalOutStreams == 0) { throw new IOException("Total output streams can't be 0"); } final long numBindPairs = totalOutStreams - 1; final BindPair[] bindPairs = new BindPair[(int) numBindPairs]; for (int i = 0; i < bindPairs.length; i++) { bindPairs[i] = new BindPair(); bindPairs[i].inIndex = readUint64(header); bindPairs[i].outIndex = readUint64(header); } folder.bindPairs = bindPairs; if (totalInStreams < numBindPairs) { throw new IOException("Total input streams can't be less than the number of bind pairs"); } final long numPackedStreams = totalInStreams - numBindPairs; final long packedStreams[] = new long[(int) numPackedStreams]; if (numPackedStreams == 1) { int i; for (i = 0; i < (int) totalInStreams; i++) { if (folder.findBindPairForInStream(i) < 0) { break; } } if (i == (int) totalInStreams) { throw new IOException("Couldn't find stream's bind pair index"); } packedStreams[0] = i; } else { for (int i = 0; i < (int) numPackedStreams; i++) { packedStreams[i] = readUint64(header); } } folder.packedStreams = packedStreams; return folder; }
From source file:dk.statsbiblioteket.util.LineReaderTest.java
public void testSample(String type, DataInput in) throws Exception { assertEquals("Int 1 should work for " + type, 12345, in.readInt()); assertEquals("Int 2 should work for " + type, -87, in.readInt()); assertEquals("Long should work for " + type, 123456789L, in.readLong()); assertEquals("String 1 should work for " + type, "Hello World!", in.readLine()); assertEquals("String 2 should work for " + type, "Another world", in.readLine()); assertEquals("Float should work for " + type, 0.5f, in.readFloat()); assertEquals("Boolean 1 should work for " + type, true, in.readBoolean()); assertEquals("Boolean 2 should work for " + type, false, in.readBoolean()); assertEquals("Byte 1 should work for " + type, (byte) 12, in.readByte()); assertEquals("Byte 2 should work for " + type, (byte) -12, in.readByte()); assertEquals("Unsigned byte should work for " + type, 129, in.readUnsignedByte()); assertEquals("Short should work for " + type, -4567, in.readShort()); byte[] loaded = new byte[5]; byte[] expected = new byte[] { (byte) 'A', (byte) 'S', (byte) 'C', (byte) 'I', (byte) 'I' }; in.readFully(loaded); for (int i = 0; i < loaded.length; i++) { assertEquals("Byte-stored string should be equal at byte " + i + " for " + type, expected[i], loaded[i]);//w w w.jav a 2s . com } }
From source file:bobs.is.compress.sevenzip.SevenZFile.java
private void readFilesInfo(final DataInput header, final Archive archive) throws IOException { final long numFiles = readUint64(header); final SevenZArchiveEntry[] files = new SevenZArchiveEntry[(int) numFiles]; for (int i = 0; i < files.length; i++) { files[i] = new SevenZArchiveEntry(); }/* w ww . j a va2 s .c o m*/ BitSet isEmptyStream = null; BitSet isEmptyFile = null; BitSet isAnti = null; while (true) { final int propertyType = header.readUnsignedByte(); if (propertyType == 0) { break; } final long size = readUint64(header); switch (propertyType) { case NID.kEmptyStream: { isEmptyStream = readBits(header, files.length); break; } case NID.kEmptyFile: { if (isEmptyStream == null) { // protect against NPE throw new IOException("Header format error: kEmptyStream must appear before kEmptyFile"); } isEmptyFile = readBits(header, isEmptyStream.cardinality()); break; } case NID.kAnti: { if (isEmptyStream == null) { // protect against NPE throw new IOException("Header format error: kEmptyStream must appear before kAnti"); } isAnti = readBits(header, isEmptyStream.cardinality()); break; } case NID.kName: { final int external = header.readUnsignedByte(); if (external != 0) { throw new IOException("Not implemented"); } if (((size - 1) & 1) != 0) { throw new IOException("File names length invalid"); } final byte[] names = new byte[(int) (size - 1)]; header.readFully(names); int nextFile = 0; int nextName = 0; for (int i = 0; i < names.length; i += 2) { if (names[i] == 0 && names[i + 1] == 0) { files[nextFile].setName(new String(names, nextName, i - nextName, CharsetNames.UTF_16LE)); nextName = i + 2; this.mapFilename.put(files[nextFile].getName(), nextFile); nextFile++; } } if (nextName != names.length || nextFile != files.length) { throw new IOException("Error parsing file names"); } break; } case NID.kCTime: { final BitSet timesDefined = readAllOrBits(header, files.length); final int external = header.readUnsignedByte(); if (external != 0) { throw new IOException("Unimplemented"); } for (int i = 0; i < files.length; i++) { files[i].setHasCreationDate(timesDefined.get(i)); if (files[i].getHasCreationDate()) { files[i].setCreationDate(Long.reverseBytes(header.readLong())); } } break; } case NID.kATime: { final BitSet timesDefined = readAllOrBits(header, files.length); final int external = header.readUnsignedByte(); if (external != 0) { throw new IOException("Unimplemented"); } for (int i = 0; i < files.length; i++) { files[i].setHasAccessDate(timesDefined.get(i)); if (files[i].getHasAccessDate()) { files[i].setAccessDate(Long.reverseBytes(header.readLong())); } } break; } case NID.kMTime: { final BitSet timesDefined = readAllOrBits(header, files.length); final int external = header.readUnsignedByte(); if (external != 0) { throw new IOException("Unimplemented"); } for (int i = 0; i < files.length; i++) { files[i].setHasLastModifiedDate(timesDefined.get(i)); if (files[i].getHasLastModifiedDate()) { files[i].setLastModifiedDate(Long.reverseBytes(header.readLong())); } } break; } case NID.kWinAttributes: { final BitSet attributesDefined = readAllOrBits(header, files.length); final int external = header.readUnsignedByte(); if (external != 0) { throw new IOException("Unimplemented"); } for (int i = 0; i < files.length; i++) { files[i].setHasWindowsAttributes(attributesDefined.get(i)); if (files[i].getHasWindowsAttributes()) { files[i].setWindowsAttributes(Integer.reverseBytes(header.readInt())); } } break; } case NID.kStartPos: { throw new IOException("kStartPos is unsupported, please report"); } case NID.kDummy: { // 7z 9.20 asserts the content is all zeros and ignores the property // Compress up to 1.8.1 would throw an exception, now we ignore it (see COMPRESS-287 if (skipBytesFully(header, size) < size) { throw new IOException("Incomplete kDummy property"); } break; } default: { // Compress up to 1.8.1 would throw an exception, now we ignore it (see COMPRESS-287 if (skipBytesFully(header, size) < size) { throw new IOException("Incomplete property of type " + propertyType); } break; } } } int nonEmptyFileCounter = 0; int emptyFileCounter = 0; for (int i = 0; i < files.length; i++) { files[i].setHasStream(isEmptyStream == null ? true : !isEmptyStream.get(i)); if (files[i].hasStream()) { files[i].setDirectory(false); files[i].setAntiItem(false); files[i].setHasCrc(archive.subStreamsInfo.hasCrc.get(nonEmptyFileCounter)); files[i].setCrcValue(archive.subStreamsInfo.crcs[nonEmptyFileCounter]); files[i].setSize(archive.subStreamsInfo.unpackSizes[nonEmptyFileCounter]); ++nonEmptyFileCounter; } else { files[i].setDirectory(isEmptyFile == null ? true : !isEmptyFile.get(emptyFileCounter)); files[i].setAntiItem(isAnti == null ? false : isAnti.get(emptyFileCounter)); files[i].setHasCrc(false); files[i].setSize(0); ++emptyFileCounter; } } archive.files = files; calculateStreamMap(archive); }
From source file:org.ambud.marauder.source.ids.pcap.layer2.EtherFrame.java
public void decode(DataInput di) throws IOException { di.readFully(this.dstMac); di.readFully(this.srcMac); this.frameTyp = extractTagType(di); //Data follows here readNetworkLayer(di);/* w w w . jav a 2 s . c o m*/ }
From source file:org.ambud.marauder.source.ids.pcap.layer2.EtherFrame.java
/** * Had to Re-define/override because of errors with LittleEndianInputStream * @param di//from ww w . j a va 2s . c o m * @return short * @throws IOException */ public static short readShort(DataInput di) throws IOException { byte[] temp = new byte[2]; di.readFully(temp); return (short) ((temp[0] << 8) | temp[1] & 0xff); }
From source file:org.ambud.marauder.source.ids.pcap.layer3.ARP.java
@Override public void decode(DataInput di, EtherFrame parent) throws IOException { this.parent = parent; this.hardType = di.readShort(); this.protoType = di.readShort(); this.hardSize = di.readByte(); this.protoAddrSize = di.readByte(); this.opCode = di.readShort(); di.readFully(senderEthAddr); this.senderIP = di.readInt(); di.readFully(targetEthAddr);/*from w ww. ja v a 2s. c o m*/ this.targetIP = di.readInt(); }