List of usage examples for java.nio MappedByteBuffer limit
@Override public final MappedByteBuffer limit(int newLimit)
From source file:gephi.spade.panel.fcsFile.java
/** * readFile ---//from www . ja va2s. c om * <p> * A helper function to read all the fields in the TEXT segment of the FCS * file. * </p> * * <p> * This helper function should only be called once by the constructor as it * is quite expensive. * </p> * * @param extractEventsP * boolean flag indicating whether to extract events in the * underlying file. * @throws <code>java.io.FileNotFoundException</code> if the file is not * found. * @throws <code>java.io.IOException</code> if an IO exception occurred. */ private void readFile(boolean extractEventsP) throws FileNotFoundException, IOException { // Open a file input stream to the file FileInputStream fis = new FileInputStream(file); // Create a byte array to hold the version byte[] versionArray = new byte[VERSION_SIZE]; // Read the version into the byte array int numRead = fis.read(versionArray); if (numRead < VERSION_SIZE) { // If the number of bytes read is less than the number of bytes in // the version string, then the file is too small to be an FCS file. isFCSP = false; // Close the file input stream fis.close(); // Quit return; } // Decode the version using the default encoding version = new String(versionArray); // Determine whether the file is an FCS file by whether the version // string starts with the FCS_PREFIX isFCSP = version.startsWith(FCS_PREFIX); if (!isFCSP) { // If the file is not an FCS file, then close the file and quit. // Close the file input stream fis.close(); // Quit return; } /** * At this point, we are pretty sure that the file is an FCS file. So, * we parse it. */ /** * Get the standard HEADER stuff */ // Skip 4 bytes to get to byte 10 fis.skip(4); // Create a byte array to hold the HEADER byte[] headerArray = new byte[48]; // Read the header into the byte array numRead = fis.read(headerArray); if (numRead < 48) { // If the number of bytes read is less than 48, then the file is too // small to be an FCS file. isFCSP = false; // Close the file input stream fis.close(); // Quit return; } try { // Try to parse the TEXT segment start and end and DATA segment // start and end textStart = Integer.parseInt((new String(headerArray, 0, 8)).trim()); textEnd = Integer.parseInt((new String(headerArray, 8, 8)).trim()); dataStart = Integer.parseInt((new String(headerArray, 16, 8)).trim()); dataEnd = Integer.parseInt((new String(headerArray, 24, 8)).trim()); } catch (NumberFormatException nfe) { // If a NumberFormatException occured, then quit because there's // nothing we can do without the TEXT or DATA segment. // Close the file input stream fis.close(); return; } /** * Get the ANALYSIS segment limits */ try { // Try to parse the analysisStart and analysisEnd analysisStart = Integer.parseInt((new String(headerArray, 32, 8)).trim()); analysisEnd = Integer.parseInt((new String(headerArray, 40, 8)).trim()); } catch (NumberFormatException nfe) { // If a NumberFormatException occured, then set the ANALYSIS start // and end to 0 since this segment is optional. analysisStart = 0; analysisEnd = 0; } /** * Use NIO to read the OTHER and TEXT segments */ // Get the channel for the input file FileChannel fc = fis.getChannel(); // Move the channel's position back to 0 fc.position(0); // Map the TEXT segment to memory MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, textEnd + 1); /** * Create the character decoder for parsing characters */ decoder = charset.newDecoder(); /** * Get the OTHER segment */ mbb.limit(textStart); mbb.position(58); CharBuffer other = decoder.decode(mbb.slice()); /** * Get the TEXT segment */ mbb.limit(textEnd + 1); mbb.position(textStart); text = decoder.decode(mbb.slice()).toString(); /** * Close the file since we have the string version of the TEXT segment */ // Close the file channel fc.close(); // Close the file input stream fis.close(); /** * Decode the TEXT segment */ // The first character of the primary TEXT segment contains the // delimiter character delimiter = text.charAt(0); /** * Key/Value Pairs */ // Generate all the pairs String[] pairs; if (delimiter == '\\') { // If the delimiter character is a backslash, then we have to escape // it in the regular expression. pairs = text.split("[\\\\]"); } else { // Otherwise, we can just split it normally by using the character // in the regular expression. pairs = text.split("[" + Character.toString(delimiter) + "]"); } /** * Calculate the number of pairs --- The number of pairs is the length * of the pairs array minus 1 divided by 2. The one is due to the empty * first element from the Java split above. */ int numPairs = (pairs.length - 1) / 2; // Create a mapping for each key and its value settings = new Properties(); // Loop through the TEXT segment we just split to get the keys and // values // The key is in (i * 2) + 1 to account for the empty first element. // The value is in (i * 2) + 2 to account for the empty first element. for (int i = 0; i < numPairs; i++) { settings.setProperty(pairs[(i * 2) + 1].trim(), pairs[(i * 2) + 2].trim()); } // Go through all the key/value pairs and parse them parseSettings(); /** * Extract Events */ if (extractEventsP) { // If we are extracting data, then do so. extractEvents(); } }
From source file:org.apache.hadoop.hdfs.hoss.db.FileBlockStore.java
/** * map a block(4KB) or a segment(256KB)//from w w w. j a v a 2 s . c o m * @param index * the index of block * @param useSegments * whether use segmeng? * @return the mapping Buffer */ @SuppressWarnings("unchecked") public final MappedByteBuffer getMmapForIndex(final int index, boolean useSegments) { if (!validState) throw new InvalidStateException(); final int mapIdx = (useSegments ? addressIndexToSegment(index) : index); final int mapSize = (useSegments ? segmentSize : blockSize); try { final Reference<MappedByteBuffer> bref = mmaps.get(mapIdx); MappedByteBuffer mbb = null; if (bref != null) { mbb = bref.get(); } if (mbb == null) { // Create mmap final long mapOffset = ((long) mapIdx * mapSize); mbb = fileChannel.map(FileChannel.MapMode.READ_WRITE, mapOffset, mapSize); // mbb.load(); mmaps.put(mapIdx, new BufferReference<MappedByteBuffer>(mapIdx, mbb)); } else { mbb.clear(); } if (useSegments) { // slice segment final int sliceBegin = (addressIndexToSegmentOffset(index) * blockSize); final int sliceEnd = (sliceBegin + blockSize); mbb.limit(sliceEnd); mbb.position(sliceBegin); mbb = (MappedByteBuffer) mbb.slice(); } return mbb; } catch (IOException e) { LOG.error("IOException in getMmapForIndex(" + index + ")", e); } return null; }
From source file:pyromaniac.IO.MMFastaImporter.java
/** * _init qual./* ww w .j a v a 2s . c om*/ * * @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 w w w .j ava 2s. com * * @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. *///from ww w .j ava2 s . 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(); } }