@Override public int readInteger() { return binaryCodec.readInt(); }
public Integer decodeRecordLength() { final int recordLength; try { recordLength = this.binaryCodec.readInt(); } catch (RuntimeEOFException e) { return null; } return recordLength; }
/** * Read an int length, and then a String of that length * @param devourNull if true, the length include a null terminator, which is read and discarded */ public String readLengthAndString(final boolean devourNull) { int length = readInt(); if (devourNull) { --length; } final String ret = readString(length); if (devourNull) { readByte(); } return ret; }
/** * Read an int length, and then a String of that length * @param devourNull if true, the length include a null terminator, which is read and discarded */ public String readLengthAndString(final boolean devourNull) { int length = readInt(); if (devourNull) { --length; } final String ret = readString(length); if (devourNull) { readByte(); } return ret; }
/** * Read an int length, and then a String of that length * @param devourNull if true, the length include a null terminator, which is read and discarded */ public String readLengthAndString(final boolean devourNull) { int length = readInt(); if (devourNull) { --length; } final String ret = readString(length); if (devourNull) { readByte(); } return ret; }
int recordLength = 0; try { recordLength = this.binaryCodec.readInt(); final int referenceID = this.binaryCodec.readInt(); final int coordinate = this.binaryCodec.readInt() + 1; final short readNameLength = this.binaryCodec.readUByte(); final short mappingQuality = this.binaryCodec.readUByte(); final int cigarLen = this.binaryCodec.readUShort(); final int flags = this.binaryCodec.readUShort(); final int readLen = this.binaryCodec.readInt(); final int mateReferenceID = this.binaryCodec.readInt(); final int mateCoordinate = this.binaryCodec.readInt() + 1; final int insertSize = this.binaryCodec.readInt(); final byte[] restOfRecord = new byte[recordLength - BAMFileConstants.FIXED_BLOCK_SIZE]; this.binaryCodec.readBytes(restOfRecord);
final int recordLength; try { recordLength = this.binaryCodec.readInt(); } catch (RuntimeEOFException e) { return null; final int referenceID = this.binaryCodec.readInt(); final int coordinate = this.binaryCodec.readInt() + 1; final short readNameLength = this.binaryCodec.readUByte(); final short mappingQuality = this.binaryCodec.readUByte(); final int cigarLen = this.binaryCodec.readUShort(); final int flags = this.binaryCodec.readUShort(); final int readLen = this.binaryCodec.readInt(); final int mateReferenceID = this.binaryCodec.readInt(); final int mateCoordinate = this.binaryCodec.readInt() + 1; final int insertSize = this.binaryCodec.readInt(); final byte[] restOfRecord = new byte[recordLength - BAMFileConstants.FIXED_BLOCK_SIZE]; this.binaryCodec.readBytes(restOfRecord);
final int recordLength; try { recordLength = this.binaryCodec.readInt(); } catch (RuntimeEOFException e) { return null; final int referenceID = this.binaryCodec.readInt(); final int coordinate = this.binaryCodec.readInt() + 1; final short readNameLength = this.binaryCodec.readUByte(); final short mappingQuality = this.binaryCodec.readUByte(); final int cigarLen = this.binaryCodec.readUShort(); final int flags = this.binaryCodec.readUShort(); final int readLen = this.binaryCodec.readInt(); final int mateReferenceID = this.binaryCodec.readInt(); final int mateCoordinate = this.binaryCodec.readInt() + 1; final int insertSize = this.binaryCodec.readInt(); final byte[] restOfRecord = new byte[recordLength - BAMFileConstants.FIXED_BLOCK_SIZE]; this.binaryCodec.readBytes(restOfRecord);
public SAMRecord decode(final int recordLength) { if (recordLength < BAMFileConstants.FIXED_BLOCK_SIZE) { throw new SAMFormatException("Invalid record length: " + recordLength); } final int referenceID = this.binaryCodec.readInt(); final int coordinate = this.binaryCodec.readInt() + 1; final short readNameLength = this.binaryCodec.readUByte(); final short mappingQuality = this.binaryCodec.readUByte(); final int bin = this.binaryCodec.readUShort(); final int cigarLen = this.binaryCodec.readUShort(); final int flags = this.binaryCodec.readUShort(); final int readLen = this.binaryCodec.readInt(); final int mateReferenceID = this.binaryCodec.readInt(); final int mateCoordinate = this.binaryCodec.readInt() + 1; final int insertSize = this.binaryCodec.readInt(); final byte[] restOfRecord = new byte[recordLength - BAMFileConstants.FIXED_BLOCK_SIZE]; this.binaryCodec.readBytes(restOfRecord); final BAMRecord ret = this.samRecordFactory.createBAMRecord( header, referenceID, coordinate, readNameLength, mappingQuality, bin, cigarLen, flags, readLen, mateReferenceID, mateCoordinate, insertSize, restOfRecord); if (null != header) { // don't reset a null header as this will clobber the reference and mate reference indices ret.setHeader(header); } return ret; } }
private static SAMSequenceRecord readSequenceRecord(final BinaryCodec stream, final String source) { final int nameLength = stream.readInt(); if (nameLength <= 1) { throw new SAMFormatException("Invalid BAM file header: missing sequence name in file " + source); } final String sequenceName = stream.readString(nameLength - 1); // Skip the null terminator stream.readByte(); final int sequenceLength = stream.readInt(); return new SAMSequenceRecord(SAMSequenceRecord.truncateSequenceName(sequenceName), sequenceLength); }
/** * Reads a single binary sequence record from the file or stream * @param source Note that this is used only for reporting errors. */ private static SAMSequenceRecord readSequenceRecord(final BinaryCodec stream, final String source) { final int nameLength = stream.readInt(); if (nameLength <= 1) { throw new SAMFormatException("Invalid BAM file header: missing sequence name in file " + source); } final String sequenceName = stream.readString(nameLength - 1); // Skip the null terminator stream.readByte(); final int sequenceLength = stream.readInt(); return new SAMSequenceRecord(SAMSequenceRecord.truncateSequenceName(sequenceName), sequenceLength); }
/** * Reads a single binary sequence record from the file or stream * @param source Note that this is used only for reporting errors. */ private static SAMSequenceRecord readSequenceRecord(final BinaryCodec stream, final String source) { final int nameLength = stream.readInt(); if (nameLength <= 1) { throw new SAMFormatException("Invalid BAM file header: missing sequence name in file " + source); } final String sequenceName = stream.readString(nameLength - 1); // Skip the null terminator stream.readByte(); final int sequenceLength = stream.readInt(); return new SAMSequenceRecord(SAMSequenceRecord.truncateSequenceName(sequenceName), sequenceLength); }
/** * Reads a single binary sequence record from the file or stream * @param source Note that this is used only for reporting errors. */ private static SAMSequenceRecord readSequenceRecord(final BinaryCodec stream, final String source) { final int nameLength = stream.readInt(); if (nameLength <= 1) { throw new SAMFormatException("Invalid BAM file header: missing sequence name in file " + source); } final String sequenceName = stream.readString(nameLength - 1); // Skip the null terminator stream.readByte(); final int sequenceLength = stream.readInt(); return new SAMSequenceRecord(SAMSequenceRecord.truncateSequenceName(sequenceName), sequenceLength); }
/** * Reads a single binary sequence record from the file or stream * @param source Note that this is used only for reporting errors. */ private static SAMSequenceRecord readSequenceRecord(final BinaryCodec stream, final String source) { final int nameLength = stream.readInt(); if (nameLength <= 1) { throw new SAMFormatException("Invalid BAM file header: missing sequence name in file " + source); } final String sequenceName = stream.readString(nameLength - 1); // Skip the null terminator stream.readByte(); final int sequenceLength = stream.readInt(); return new SAMSequenceRecord(SAMSequenceRecord.truncateSequenceName(sequenceName), sequenceLength); }
@Test public void testReadAndWriteInt() throws IOException { final int value = 42; //Writing to file final File outputFile = File.createTempFile(TEST_BASENAME, ".bin"); outputFile.deleteOnExit(); final OutputStream stream = new FileOutputStream(outputFile); final BinaryCodec codec = new BinaryCodec(stream); codec.writeInt(value); codec.close(); //Reading from file final InputStream instream = new FileInputStream(outputFile); final BinaryCodec readCodec = new BinaryCodec(instream); Assert.assertEquals(value, readCodec.readInt()); readCodec.close(); }
@Test public void testReadAndWriteString() throws IOException { final String value = "Test String to Write"; //Writing to file final File outputFile = File.createTempFile(TEST_BASENAME, ".bin"); outputFile.deleteOnExit(); final OutputStream stream = new FileOutputStream(outputFile); final BinaryCodec codec = new BinaryCodec(stream); codec.writeString(value, true, false); codec.close(); //Reading from file final InputStream instream = new FileInputStream(outputFile); final BinaryCodec readCodec = new BinaryCodec(instream); final int stringLength = readCodec.readInt(); Assert.assertEquals(value.length(), stringLength); final String s = readCodec.readString(stringLength); Assert.assertEquals(value, s); readCodec.close(); }
@Test public void readPastEndOfFile() throws IOException{ final long startTime = System.currentTimeMillis(); int i = 0; final File outputFile = File.createTempFile(TEST_BASENAME, ".bin"); outputFile.deleteOnExit(); final OutputStream stream = new FileOutputStream(outputFile); final BinaryCodec codec = new BinaryCodec(stream); while (i<100){ codec.writeInt(i); i++; } codec.close(); final InputStream instream = new FileInputStream(outputFile); final BinaryCodec readCodec = new BinaryCodec(instream); System.out.println((System.currentTimeMillis() - startTime) + "ms to write"); int z = 0; boolean reachedStatement = false; while (z<1000) { try { Assert.assertEquals(z, readCodec.readInt()); } catch (Exception e) { Assert.assertEquals(RuntimeEOFException.class, e.getClass()); reachedStatement = true; } z++; } Assert.assertTrue(reachedStatement); readCodec.close(); }
@Test public void timeTest() throws IOException{ final long startTime = System.currentTimeMillis(); int i = 0; final File outputFile = File.createTempFile(TEST_BASENAME, ".bin"); outputFile.deleteOnExit(); final OutputStream stream = new FileOutputStream(outputFile); final BinaryCodec codec = new BinaryCodec(stream); while (i<100){ codec.writeInt(i); i++; } codec.close(); final InputStream instream = new FileInputStream(outputFile); final BinaryCodec readCodec = new BinaryCodec(instream); System.out.println((System.currentTimeMillis() - startTime) + "ms to write"); int z = 0; boolean reachedStatement = false; while (z<1000) { try { Assert.assertEquals(z, readCodec.readInt()); } catch (Exception e) { Assert.assertEquals(RuntimeEOFException.class, e.getClass()); reachedStatement = true; } z++; } Assert.assertTrue(reachedStatement); readCodec.close(); }
@Test public void testReadAndWriteMutlitpleData() throws IOException{ final float fValue = 42.5F; final String sValue = "TestString"; final File outputFile = File.createTempFile(TEST_BASENAME, ".bin"); outputFile.deleteOnExit(); final OutputStream stream = new FileOutputStream(outputFile); final BinaryCodec codec = new BinaryCodec(stream); codec.writeFloat(fValue); codec.writeString(sValue, true, false); codec.close(); //Reading from file final InputStream instream = new FileInputStream(outputFile); final BinaryCodec readCodec = new BinaryCodec(instream); Assert.assertEquals(fValue, readCodec.readFloat()); final int stringLength = readCodec.readInt(); Assert.assertEquals(sValue, readCodec.readString(stringLength)); readCodec.close(); }
final int headerTextLength = stream.readInt(); final String textHeader = stream.readString(headerTextLength); final SAMTextHeaderCodec headerCodec = new SAMTextHeaderCodec(); final SAMFileHeader samFileHeader = headerCodec.decode(new StringLineReader(textHeader), source); final int sequenceCount = stream.readInt(); if (samFileHeader.getSequenceDictionary().size() > 0) {