protected BAMFileWriter(final OutputStream os, final String absoluteFilename, final int compressionLevel, final DeflaterFactory deflaterFactory) { blockCompressedOutputStream = new BlockCompressedOutputStream(os, (Path)null, compressionLevel, deflaterFactory); outputBinaryCodec = new BinaryCodec(blockCompressedOutputStream); outputBinaryCodec.setOutputFileName(absoluteFilename); }
/** * Writes a string to the buffer as ASCII bytes * * @param value string to write to buffer * @param writeLength prefix the string with the length as a 32-bit int * @param appendNull add a null byte to the end of the string */ public void writeString(final String value, final boolean writeLength, final boolean appendNull) { if (writeLength) { int lengthToWrite = value.length(); if (appendNull) lengthToWrite++; writeInt(lengthToWrite); } //Actually writes the string to a buffer writeString(value); if (appendNull) writeBytes(NULL_BYTE); }
/** * Any necessary processing at the end of the file */ @Override public void close() { codec.close(); }
private void writeHeader() { // magic string final byte[] magic = BAMFileConstants.BAM_INDEX_MAGIC; codec.writeBytes(magic); codec.writeInt(nRef); }
/** * Sets the input stream that records will be read from. */ public void setInputStream(final InputStream is, final String filename) { this.binaryCodec.setInputStream(is); this.binaryCodec.setInputFileName(filename); }
/** * 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; }
@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(); }
@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 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(); }
codec.writeInt(size + ((metaData != null)? 1 : 0 )); final int indexStart = linearIndex == null ? 0 : linearIndex.getIndexStart(); final int n_intv = entries == null ? indexStart : entries.length + indexStart; codec.writeInt(n_intv); if (entries == null) { return; codec.writeLong(0); codec.writeLong(entries[k]); codec.getOutputStream().flush(); } catch (final IOException e) { throw new SAMException("IOException in BinaryBAMIndexWriter reference " + content.getReferenceSequence(), e);
@Test public void testReadAndWriteLong() throws IOException { final long value = 42; final File outputFile = File.createTempFile(TEST_BASENAME, ".bin"); outputFile.deleteOnExit(); final OutputStream stream = new FileOutputStream(outputFile); final BinaryCodec codec = new BinaryCodec(stream); codec.writeLong(value); codec.close(); //Reading from file final InputStream instream = new FileInputStream(outputFile); final BinaryCodec readCodec = new BinaryCodec(instream); Assert.assertEquals(value, readCodec.readLong()); readCodec.close(); }
/** * Writes a byte array to the output buffer * * @param bytes value to write */ public void writeBytes(final byte[] bytes) { writeBytes(bytes, 0, bytes.length); }
stream.readBytes(buffer); if (!Arrays.equals(buffer, "BAM\1".getBytes())) { throw new IOException("Invalid BAM file header"); final int headerTextLength = stream.readInt(); final String textHeader = stream.readString(headerTextLength); final SAMTextHeaderCodec headerCodec = new SAMTextHeaderCodec(); headerCodec.setValidationStringency(validationStringency); final SAMFileHeader samFileHeader = headerCodec.decode(new StringLineReader(textHeader), source); final int sequenceCount = stream.readInt(); if (samFileHeader.getSequenceDictionary().size() > 0) {
/** * Write the meta data represented by the chunkLists associated with bin MAX_BINS 37450 * * @param metaData information describing numAligned records, numUnAligned, etc */ private void writeChunkMetaData(final BAMIndexMetaData metaData) { codec.writeInt(GenomicIndexUtil.MAX_BINS); final int nChunk = 2; codec.writeInt(nChunk); codec.writeLong(metaData.getFirstOffset()); codec.writeLong(metaData.getLastOffset()); codec.writeLong(metaData.getAlignedRecordCount()); codec.writeLong(metaData.getUnalignedRecordCount()); }
@Test public void testReadAndWriteDouble() throws IOException { final double value = 54.4; final File outputFile = File.createTempFile(TEST_BASENAME, ".bin"); outputFile.deleteOnExit(); final OutputStream stream = new FileOutputStream(outputFile); final BinaryCodec codec = new BinaryCodec(stream); codec.writeDouble(value); codec.close(); //Reading from file final InputStream instream = new FileInputStream(outputFile); final BinaryCodec readCodec = new BinaryCodec(instream); Assert.assertEquals(value, readCodec.readDouble()); readCodec.close(); }
public Bam_OBA_Supplier(BufferedInputStream is) { this.is = is; codec = new BinaryCodec(); codec.setInputStream(is); baos = new ByteArrayOutputStream(); refId = Integer.MIN_VALUE; recordCounter = 0; }
/** * Prepare to compress at the given compression level * @param compressionLevel 1 <= compressionLevel <= 9 */ public BlockCompressedOutputStream(final File file, final int compressionLevel) { this.file = file; codec = new BinaryCodec(file, true); deflater = DeflaterFactory.makeDeflater(compressionLevel, true); }
/** * Read a byte array from the input stream. * * @throws htsjdk.samtools.util.RuntimeEOFException if fewer than buffer.length bytes to read */ public void readBytes(final byte[] buffer) { readBytes(buffer, 0, buffer.length); }
String headerb = "00110011000000011001000000000001001011000000000100000010000000000000000000000000000000000000000000000000000000000000000000000000"; BinaryCodec codec = new BinaryCodec(); //I have no idea why this method is not static. //you may use BinaryCodec.fromAscii(ascii.toCharArray()) instead byte[] bval = codec.toByteArray(headerb); File file = new File("test.epd"); Files.write(bval, file ); //Test that when the file is read, we retrieve the same string byte[] byteArray = Files.toByteArray(file); String asciiString = BinaryCodec.toAsciiString(byteArray); System.out.println(asciiString);
/** * WARNING: flush() affects the output format, because it causes the current contents of uncompressedBuffer * to be compressed and written, even if it isn't full. Unless you know what you're doing, don't call flush(). * Instead, call close(), which will flush any unwritten data before closing the underlying stream. * */ @Override public void flush() throws IOException { while (numUncompressedBytes > 0) { deflateBlock(); } codec.getOutputStream().flush(); }