Skip to content

Instantly share code, notes, and snippets.

@s4553711
Last active May 31, 2018 09:45
Show Gist options
  • Save s4553711/1db43bbe74354ddb7b5ff5afaf73bb9a to your computer and use it in GitHub Desktop.
Save s4553711/1db43bbe74354ddb7b5ff5afaf73bb9a to your computer and use it in GitHub Desktop.
package com.ck.fastq.util;
/**
* Created by s4553711 on 2018/5/31.
*/
public class FastqReader {
private StdinIterator reader;
private int head = 0;
private int mark = 0;
private int tail = 0;
private byte[] buf = new byte[35 * 1024 * 1024];
public FastqReader(StdinIterator input) {
reader = input;
}
public boolean hasNext() {
boolean fetching = true;
while(fetching) {
fill();
}
}
public byte[] Next() {
}
private void fill() {
if (!reader.hasNext()) return;
byte[] tmp = reader.Next();
if (head == 0 && tail == 0) {
System.arraycopy(tmp, 0, buf, 0, tmp.length);
mark = head = 0;
tail = tmp.length - 1;
} else if (tail - mark < 500) {
byte[] tmp_buf = new byte[35 * 1024 * 1024];
System.arraycopy(buf, head, tmp_buf, 0, tail - head + 1);
System.arraycopy(tmp, 0, tmp_buf, tail - head + 1, tmp.length);
tail = (tail - head + tmp.length);
mark = mark - head;
head = 0;
buf = tmp_buf;
}
}
}
package com.ck.fastq;
import com.ck.fastq.util.StdinIterator;
/**
* Created by s4553711 on 2018/5/31.
*/
public class IteratorTest {
public static void main(String[] args) {
StdinIterator inp = new StdinIterator();
long st = System.currentTimeMillis();
long st_s = st;
long accum = 0l;
long accum_tot = accum;
long counter = 0l;
while(inp.hasNext()) {
byte[] data = inp.Next();
// System.err.format("log > Receive length: %10d / %d\n",data.length,counter);
// for(byte b : data) {
// System.out.print((char)b);
// }
counter++;
accum += data.length;
accum_tot += data.length;
if (counter % 500 == 0) {
System.err.format("Log > count: %d, rate: %6.1f MB/s\n",counter,
(accum/(1024*1024))/((System.currentTimeMillis() - st)/1000.0));
st = System.currentTimeMillis();
accum = 0l;
}
}
System.err.format("Log > Avg rate %6.1f MS/s, %8.2f MB, %8.0f s\n",(accum_tot/(1024*1024))/((System.currentTimeMillis() - st_s)/1000.0),
accum_tot/(1024.0*1024.0), (System.currentTimeMillis() - st_s)/1000.0);
}
}
package com.ck.fastq.util;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Arrays;
public class StdinIterator {
private BufferedInputStream bis;
private byte[] buf = new byte[10 * 1024 * 1024];
private int nRead;
public StdinIterator() {
bis = new BufferedInputStream(new BufferedInputStream(System.in, 8 * 1024));
}
public boolean hasNext() {
try {
nRead = bis.read(buf, 0, buf.length);
} catch (IOException e) {
e.printStackTrace();
}
return nRead != -1;
}
public byte[] Next() {
return Arrays.copyOfRange(buf, 0, nRead);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment