Example usage for java.nio CharBuffer capacity

List of usage examples for java.nio CharBuffer capacity

Introduction

In this page you can find the example usage for java.nio CharBuffer capacity.

Prototype

public final int capacity() 

Source Link

Document

Returns the capacity of this buffer.

Usage

From source file:com.asakusafw.runtime.io.csv.CsvParser.java

private void emit(int c) throws IOException {
    assert c >= 0;
    CharBuffer buf = lineBuffer;
    if (buf.remaining() == 0) {
        if (buf.capacity() == BUFFER_LIMIT) {
            throw new IOException(
                    MessageFormat.format("Line is too large (near {0}:{1}, size={2}, record-number={3})", path,
                            currentPhysicalHeadLine, BUFFER_LIMIT, currentRecordNumber));
        }//from   w w w. java 2 s.c  o m
        CharBuffer newBuf = CharBuffer.allocate(Math.min(buf.capacity() * 2, BUFFER_LIMIT));
        newBuf.clear();
        buf.flip();
        newBuf.put(buf);
        buf = newBuf;
        lineBuffer = newBuf;
    }
    buf.put((char) c);
}

From source file:pyromaniac.IO.MMFastaImporter.java

/**
 * _init qual.//from  w w  w .j  a  v a2 s .  c o m
 *
 * @throws Exception the exception
 */
private void _initQual() throws Exception {
    FileInputStream tempStream = new FileInputStream(new File(this.qualFile));
    FileChannel fcQual = tempStream.getChannel();
    this.qualSizeLong = fcQual.size();

    //qual starts LL contains pairs, marking file #no (in  qualBuffers) and position #no (in the buffer).
    this.qualStartsLL = new ArrayList<Pair<Integer, Long>>();

    for (long startPosition = 0L; startPosition < this.qualSizeLong; startPosition += HALF_GIGA) {
        MappedByteBuffer qualBuffer = fcQual.map(FileChannel.MapMode.READ_ONLY, startPosition,
                Math.min(this.qualSizeLong - startPosition, HALF_GIGA)); //map half a gig to this channel.
        this.qualBuffers.add(qualBuffer);
        int qbf_pos = qualBuffers.size() - 1;
        int maxBuffer = 2048;
        int bufferSize = (qualBuffer.capacity() > maxBuffer) ? maxBuffer : qualBuffer.capacity();

        qualBuffer.limit(bufferSize);
        qualBuffer.position(0);

        while (qualBuffer.position() != qualBuffer.capacity()) {
            int prevPos = qualBuffer.position();
            CharBuffer result = decoder.decode(qualBuffer);
            qualBuffer.position(prevPos);

            for (int i = 0; i < result.capacity(); i++) {
                char curr = result.charAt(i);
                int posInFile = prevPos + i;

                if (curr == BEGINNING_FASTA_HEADER) {
                    qualStartsLL.add(new Pair<Integer, Long>(qbf_pos, new Long(posInFile)));
                }
            }

            int newPos = qualBuffer.limit();

            if (qualBuffer.limit() + bufferSize > qualBuffer.capacity())
                qualBuffer.limit(qualBuffer.capacity());
            else
                qualBuffer.limit(qualBuffer.limit() + bufferSize);
            qualBuffer.position(newPos);
        }
        qualBuffer.rewind();
    }
}

From source file:pyromaniac.IO.MMFastaImporter.java

/**
 * _init seq./*from www .ja va 2  s.c o  m*/
 *
 * @throws Exception the exception
 */
private void _initSeq() throws Exception {
    FileInputStream tempStream = new FileInputStream(new File(this.seqFile));
    FileChannel fcSeq = tempStream.getChannel();
    this.seqSizeLong = fcSeq.size();
    this.seqStartsLL = new ArrayList<Pair<Integer, Long>>();

    for (long startPosition = 0L; startPosition < this.seqSizeLong; startPosition += HALF_GIGA) {
        MappedByteBuffer seqBuffer = fcSeq.map(FileChannel.MapMode.READ_ONLY, startPosition,
                Math.min(this.seqSizeLong - startPosition, HALF_GIGA));

        this.seqBuffers.add(seqBuffer);
        int sbf_pos = seqBuffers.size() - 1;
        int maxBuffer = 2048;
        int bufferSize = (seqBuffer.capacity() > maxBuffer) ? maxBuffer : seqBuffer.capacity();

        seqBuffer.limit(bufferSize);
        seqBuffer.position(0);

        while (seqBuffer.position() != seqBuffer.capacity()) {
            int prevPos = seqBuffer.position();
            CharBuffer result = decoder.decode(seqBuffer);
            seqBuffer.position(prevPos);

            for (int i = 0; i < result.capacity(); i++) {
                char curr = result.charAt(i);
                int posInFile = prevPos + i;

                if (curr == BEGINNING_FASTA_HEADER) {
                    seqStartsLL.add(new Pair<Integer, Long>(sbf_pos, new Long(posInFile)));
                }
            }

            int newPos = seqBuffer.limit();

            if (seqBuffer.limit() + bufferSize > seqBuffer.capacity())
                seqBuffer.limit(seqBuffer.capacity());
            else
                seqBuffer.limit(seqBuffer.limit() + bufferSize);
            seqBuffer.position(newPos);
        }
        seqBuffer.rewind();
    }

}

From source file:pyromaniac.IO.MMFastqImporter.java

/**
 * Helper function for init(). Scans this.fastq file for sequence starts and records their position.
 * Multiple MappedByteBuffers are used to handle large files.
 * @throws Exceptions relating to file reading and decoding.
 *///  w  w  w . ja  v  a 2s  .  c om
private void _initFile() throws Exception {
    FileInputStream tempStream = new FileInputStream(new File(this.fastqFile));
    FileChannel fcSeq = tempStream.getChannel();
    this.seqSizeLong = fcSeq.size();
    this.recordStarts = new ArrayList<Pair<Integer, Long>>();

    int state = -1;

    for (long startPosition = 0L; startPosition < this.seqSizeLong; startPosition += HALF_GIGA) {
        MappedByteBuffer recordBuffer = fcSeq.map(FileChannel.MapMode.READ_ONLY, startPosition,
                Math.min(this.seqSizeLong - startPosition, HALF_GIGA));
        this.recordBuffers.add(recordBuffer);

        int sbf_pos = this.recordBuffers.size() - 1;

        int maxBuffer = 2048;
        int bufferSize = (recordBuffer.capacity() > maxBuffer) ? maxBuffer : recordBuffer.capacity();

        recordBuffer.limit(bufferSize);
        recordBuffer.position(0);

        while (recordBuffer.position() != recordBuffer.capacity()) {
            int prevPos = recordBuffer.position();
            CharBuffer result = decoder.decode(recordBuffer);
            recordBuffer.position(prevPos);

            for (int i = 0; i < result.capacity(); i++) {
                char curr = result.charAt(i);
                int posInFile = prevPos + i;

                //I see a fastq header, I am either at beginning of file, or last saw the quality line...
                if (curr == BEGINNING_FASTQ_SEQ && (state == -1 || state == 4)) {
                    this.recordStarts.add(new Pair<Integer, Long>(sbf_pos, new Long(posInFile)));
                    state = 1;

                } else if (curr == BEGINNING_FASTQ_QUAL && (state == 1)) {
                    state = 2;
                } else if ((curr == '\n' || curr == '\r') & state == 2) {
                    state = 3;
                } else if ((curr == '\n' || curr == '\r') & state == 3) {
                    state = 4;
                }
            }

            int newPos = recordBuffer.limit();

            if (recordBuffer.limit() + bufferSize > recordBuffer.capacity())
                recordBuffer.limit(recordBuffer.capacity());
            else
                recordBuffer.limit(recordBuffer.limit() + bufferSize);
            recordBuffer.position(newPos);
        }
        recordBuffer.rewind();
    }
}