Java Code Examples for java.nio.MappedByteBuffer#getChar()
The following examples show how to use
java.nio.MappedByteBuffer#getChar() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Zip.java From turbine with Apache License 2.0 | 5 votes |
private byte[] getBytes( long offset, int nameLength, int cenExtLength, long size, boolean deflate) { if (size > Integer.MAX_VALUE) { throw new IllegalArgumentException("unsupported zip entry size: " + size); } try { MappedByteBuffer fc = chan.map( MapMode.READ_ONLY, offset, Math.min( LOCHDR + nameLength + cenExtLength + size + EXTRA_FIELD_SLACK, chan.size() - offset)); fc.order(ByteOrder.LITTLE_ENDIAN); checkSignature(path, fc, /* index= */ 0, 3, 4, "LOCSIG"); int locExtLength = fc.getChar(LOCEXT); if (locExtLength > cenExtLength + EXTRA_FIELD_SLACK) { // If the local header's extra fields don't match the central directory and we didn't // leave enough slac, re-map the data section with the correct extra field length. fc = chan.map(MapMode.READ_ONLY, offset + LOCHDR + nameLength + locExtLength, size); fc.order(ByteOrder.LITTLE_ENDIAN); } else { // Otherwise seek past the local header, name, and extra fields to the data. fc.position(LOCHDR + nameLength + locExtLength); fc.limit((int) (LOCHDR + nameLength + locExtLength + size)); } byte[] bytes = new byte[(int) size]; fc.get(bytes); if (deflate) { bytes = ByteStreams.toByteArray( new InflaterInputStream( new ByteArrayInputStream(bytes), new Inflater(/*nowrap=*/ true))); } return bytes; } catch (IOException e) { throw new IOError(e); } }
Example 2
Source File: TestFileMap.java From mynlp with Apache License 2.0 | 4 votes |
public static void main2(String[] args) throws Exception { //FileChannel.open() RandomAccessFile f = new RandomAccessFile("testdata/float.martix", "rw"); FileChannel fileChannel = f.getChannel(); MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, fileChannel.size()); buffer.getInt(); System.out.println(buffer); buffer.getLong(); System.out.println(buffer); buffer.getChar(); System.out.println(buffer); float[] floats = new float[300]; buffer.asFloatBuffer().get(floats); System.out.println(Arrays.toString(floats)); System.out.println(buffer); // FloatBuffer floatBuffer = buffer.asFloatBuffer(); // // // float[] floats = new float[300]; // // floatBuffer.position(0); // floatBuffer.get(floats); // System.out.println(floatBuffer); // System.out.println(Arrays.toString(floats)); // System.out.println(buffer); // // // // System.out.println("----"+buffer.asLongBuffer()); // System.out.println(buffer.asFloatBuffer()); // System.out.println(buffer.position(1200)); // System.out.println(buffer.slice().get(0)); // Random random = new Random(); // // long t1 = System.currentTimeMillis(); // for (int i = 0; i < 1000000; i++) { // floatBuffer.position(random.nextInt(1000000)*300); // floatBuffer.get(floats); // } // long t2 = System.currentTimeMillis(); // // System.out.println(t2-t1); //TimeUnit.SECONDS.sleep(10); fileChannel.close(); }
Example 3
Source File: Zip.java From turbine with Apache License 2.0 | 4 votes |
public ZipIterable(Path path) throws IOException { this.path = path; this.chan = FileChannel.open(path, StandardOpenOption.READ); // Locate the EOCD long size = chan.size(); if (size < ENDHDR) { throw new ZipException("invalid zip archive"); } long eocdOffset = size - ENDHDR; MappedByteBuffer eocd = chan.map(MapMode.READ_ONLY, eocdOffset, ENDHDR); eocd.order(ByteOrder.LITTLE_ENDIAN); int index = 0; int commentSize = 0; if (!isSignature(eocd, 0, 5, 6)) { // The archive may contain a zip file comment; keep looking for the EOCD. long start = Math.max(0, size - ENDHDR - 0xFFFF); eocd = chan.map(MapMode.READ_ONLY, start, (size - start)); eocd.order(ByteOrder.LITTLE_ENDIAN); index = (int) ((size - start) - ENDHDR); while (index > 0) { index--; eocd.position(index); if (isSignature(eocd, index, 5, 6)) { commentSize = (int) ((size - start) - ENDHDR) - index; eocdOffset = start + index; break; } } } checkSignature(path, eocd, index, 5, 6, "ENDSIG"); int totalEntries = eocd.getChar(index + ENDTOT); long cdsize = UnsignedInts.toLong(eocd.getInt(index + ENDSIZ)); int actualCommentSize = eocd.getChar(index + ENDCOM); if (commentSize != actualCommentSize) { throw new ZipException( String.format( "zip file comment length was %d, expected %d", commentSize, actualCommentSize)); } // If the number of entries is 0xffff, check if the archive has a zip64 EOCD locator. if (totalEntries == ZIP64_MAGICCOUNT) { // Assume the zip64 EOCD has the usual size; we don't support zip64 extensible data sectors. long zip64eocdOffset = size - ENDHDR - ZIP64_LOCHDR - ZIP64_ENDHDR; MappedByteBuffer zip64eocd = chan.map(MapMode.READ_ONLY, zip64eocdOffset, ZIP64_ENDHDR); zip64eocd.order(ByteOrder.LITTLE_ENDIAN); // Note that zip reading is necessarily best-effort, since an archive could contain 0xFFFF // entries and the last entry's data could contain a ZIP64_ENDSIG. Some implementations // read the full EOCD records and compare them. if (zip64eocd.getInt(0) == ZIP64_ENDSIG) { cdsize = zip64eocd.getLong(ZIP64_ENDSIZ); eocdOffset = zip64eocdOffset; } } this.cd = chan.map(MapMode.READ_ONLY, eocdOffset - cdsize, cdsize); cd.order(ByteOrder.LITTLE_ENDIAN); }