Skip to content

Commit

Permalink
Change non-public top-level classes to be nested classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwhite committed Feb 7, 2018
1 parent 80d964f commit e59cdec
Show file tree
Hide file tree
Showing 5 changed files with 334 additions and 334 deletions.
120 changes: 60 additions & 60 deletions src/main/java/org/seqdoop/hadoop_bam/BCFRecordReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,77 +177,77 @@ public boolean nextKeyValue() throws IOException {
vc.set(v);
return true;
}
}

class BGZFLimitingStream extends InputStream {

private final BlockCompressedInputStream bgzf;
private final long virtEnd;
private byte[] readBuf = new byte[1];

public BGZFLimitingStream(BlockCompressedInputStream stream, long virtualEnd) {
bgzf = stream;
virtEnd = virtualEnd;
}
static class BGZFLimitingStream extends InputStream {

@Override
public void close() throws IOException {
bgzf.close();
}
private final BlockCompressedInputStream bgzf;
private final long virtEnd;
private byte[] readBuf = new byte[1];

@Override
public int read() throws IOException {
switch (read(readBuf)) {
case 1:
return readBuf[0];
case -1:
return -1;
default:
assert false;
return -1;
public BGZFLimitingStream(BlockCompressedInputStream stream, long virtualEnd) {
bgzf = stream;
virtEnd = virtualEnd;
}
}

@Override
public int read(byte[] buf, int off, int len) throws IOException {

int totalRead = 0;
long virt;

final int lastLen = (int) virtEnd & 0xffff;

while ((virt = bgzf.getFilePointer()) >>> 16 != virtEnd >>> 16) {
// We're not in the last BGZF block yet. Unfortunately
// BlockCompressedInputStream doesn't expose the length of the current
// block, so we can't simply (possibly repeatedly) read the current
// block to the end. Instead, we read at most virtEnd & 0xffff at a
// time, which ensures that we can't overshoot virtEnd even if the
// next block starts immediately.
final int r = bgzf.read(buf, off, Math.min(len, lastLen));
if (r == -1) {
return totalRead == 0 ? -1 : totalRead;
}
@Override
public void close() throws IOException {
bgzf.close();
}

totalRead += r;
len -= r;
if (len == 0) {
return totalRead;
@Override
public int read() throws IOException {
switch (read(readBuf)) {
case 1:
return readBuf[0];
case -1:
return -1;
default:
assert false;
return -1;
}
off += r;
}

// We're in the last BGZF block: read only up to lastLen.
len = Math.min(len, ((int) virt & 0xffff) - lastLen);
while (len > 0) {
final int r = bgzf.read(buf, off, len);
if (r == -1) {
return totalRead == 0 ? -1 : totalRead;
@Override
public int read(byte[] buf, int off, int len) throws IOException {

int totalRead = 0;
long virt;

final int lastLen = (int) virtEnd & 0xffff;

while ((virt = bgzf.getFilePointer()) >>> 16 != virtEnd >>> 16) {
// We're not in the last BGZF block yet. Unfortunately
// BlockCompressedInputStream doesn't expose the length of the current
// block, so we can't simply (possibly repeatedly) read the current
// block to the end. Instead, we read at most virtEnd & 0xffff at a
// time, which ensures that we can't overshoot virtEnd even if the
// next block starts immediately.
final int r = bgzf.read(buf, off, Math.min(len, lastLen));
if (r == -1) {
return totalRead == 0 ? -1 : totalRead;
}

totalRead += r;
len -= r;
if (len == 0) {
return totalRead;
}
off += r;
}

totalRead += r;
len -= r;
off += r;
// We're in the last BGZF block: read only up to lastLen.
len = Math.min(len, ((int) virt & 0xffff) - lastLen);
while (len > 0) {
final int r = bgzf.read(buf, off, len);
if (r == -1) {
return totalRead == 0 ? -1 : totalRead;
}

totalRead += r;
len -= r;
off += r;
}
return totalRead == 0 ? -1 : totalRead;
}
return totalRead == 0 ? -1 : totalRead;
}
}
86 changes: 43 additions & 43 deletions src/main/java/org/seqdoop/hadoop_bam/BCFRecordWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,57 +122,57 @@ protected void writeRecord(VariantContext vc) {

writer.add(vc);
}
}

// We must always call writer.writeHeader() because the writer requires
// the header in writer.add(), and writeHeader() is the only way to give
// the header to the writer. Thus, we use this class to simply throw away
// output until after the header's been written.
//
// This is, of course, a HACK and a slightly dangerous one: if writer
// does any buffering of its own and doesn't flush after writing the
// header, this isn't as easy as this.
//
// In addition we do BGZF compression here, to simplify things.
final class BCFStoppableOutputStream extends FilterOutputStream {

private final OutputStream origOut;
public boolean stopped;

public BCFStoppableOutputStream(boolean startStopped, OutputStream out) {
super(new BlockCompressedOutputStream(out, null));
origOut = out;
stopped = startStopped;
}
// We must always call writer.writeHeader() because the writer requires
// the header in writer.add(), and writeHeader() is the only way to give
// the header to the writer. Thus, we use this class to simply throw away
// output until after the header's been written.
//
// This is, of course, a HACK and a slightly dangerous one: if writer
// does any buffering of its own and doesn't flush after writing the
// header, this isn't as easy as this.
//
// In addition we do BGZF compression here, to simplify things.
static final class BCFStoppableOutputStream extends FilterOutputStream {

private final OutputStream origOut;
public boolean stopped;

public BCFStoppableOutputStream(boolean startStopped, OutputStream out) {
super(new BlockCompressedOutputStream(out, null));
origOut = out;
stopped = startStopped;
}

@Override
public void write(int b) throws IOException {
if (!stopped) {
super.write(b);
@Override
public void write(int b) throws IOException {
if (!stopped) {
super.write(b);
}
}
}

@Override
public void write(byte[] b) throws IOException {
if (!stopped) {
super.write(b);
@Override
public void write(byte[] b) throws IOException {
if (!stopped) {
super.write(b);
}
}
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
if (!stopped) {
super.write(b, off, len);
@Override
public void write(byte[] b, int off, int len) throws IOException {
if (!stopped) {
super.write(b, off, len);
}
}
}

@Override
public void close() throws IOException {
// Don't close the BlockCompressedOutputStream, as we don't want
// the BGZF terminator.
this.out.flush();
@Override
public void close() throws IOException {
// Don't close the BlockCompressedOutputStream, as we don't want
// the BGZF terminator.
this.out.flush();

// Instead, close the lower-level output stream directly.
origOut.close();
// Instead, close the lower-level output stream directly.
origOut.close();
}
}
}
Loading

0 comments on commit e59cdec

Please sign in to comment.