Java Code Examples for java.nio.MappedByteBuffer#limit()
The following examples show how to use
java.nio.MappedByteBuffer#limit() .
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: TestChannel.java From code with Apache License 2.0 | 6 votes |
@Test public void test2() { long start = System.currentTimeMillis(); try ( //1.获取通道 FileChannel inChannel = FileChannel.open(Paths.get("d:/59035.zip"), StandardOpenOption.READ); FileChannel outChannel = FileChannel.open(Paths.get("d:/59036.zip"), StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);) { //2.内存映射文件 MappedByteBuffer inMappedBuf = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size()); MappedByteBuffer outMappedBuf = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size()); //3.直接对缓冲区进行数据的读写操作 byte[] dst = new byte[inMappedBuf.limit()]; inMappedBuf.get(dst); outMappedBuf.put(dst); } catch (IOException e) { e.printStackTrace(); } long end = System.currentTimeMillis(); System.out.println("耗费的时间为:" + (end - start));//耗费的时间为:200 }
Example 2
Source File: AntiCrashCrimeScene.java From antsdb with GNU Lesser General Public License v3.0 | 6 votes |
private synchronized void grow() { try { long start = this.units.size() * Unit.SIZE; MappedByteBuffer mmf = this.ch.map(MapMode.READ_WRITE, start, Unit.SIZE * 100); this.mmfs.add(mmf); mmf.order(ByteOrder.nativeOrder()); for (int i=0; i<100; i++) { Unit ii = new Unit(); mmf.position(i * Unit.SIZE); mmf.limit(mmf.position() + Unit.SIZE); ii.buf = mmf.slice(); ii.addr = UberUtil.getAddress(ii.buf); this.units.add(ii); } } catch (Exception ignored) { // if it fails then nothing is logged } }
Example 3
Source File: NIODemo2.java From cs-summary-reflection with Apache License 2.0 | 5 votes |
@Test public void test2() throws IOException { long start = System.currentTimeMillis(); // myeclipse2013.exe 1G -> out of memory 因为是直接使用内存 我的机器是4G可用不足 // 使用小的图片测试 FileChannel inChannel = FileChannel.open(Paths.get("g:/复杂度.png"), StandardOpenOption.READ); FileChannel outChannel = FileChannel.open( Paths.get("g:/JAVA_PROJECT/复杂度.png"), StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE); // 内存映射文件 MappedByteBuffer inMappedBuf = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size()); MappedByteBuffer outMappedBuf = outChannel.map(MapMode.READ_WRITE, 0, inChannel.size()); // 直接对缓冲区进行数据的读写操作 byte[] dst = new byte[inMappedBuf.limit()]; inMappedBuf.get(dst); outMappedBuf.put(dst); inChannel.close(); outChannel.close(); long end = System.currentTimeMillis(); System.out.println("耗费时间为:" + (end - start)); }
Example 4
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 5
Source File: AntiCrashCrimeScene.java From antsdb with GNU Lesser General Public License v3.0 | 5 votes |
private void open(boolean write) throws IOException { if (this.raf != null) { return; } // rename the existing file so we dont accidently lose previous crash scene if (write && file.exists()) { String filename = file.getName(); filename = System.currentTimeMillis() + "-" + filename; File backup = new File(file.getParentFile(), filename); file.renameTo(backup); } // open it this.raf = new RandomAccessFile(file, write ? "rw" : "r"); this.ch = this.raf.getChannel(); // map units if this is read only if (!write) { int nbuffers = (int)(this.file.length() / Unit.SIZE); MappedByteBuffer mmf = raf.getChannel().map(MapMode.READ_ONLY, 0, Unit.SIZE * nbuffers); this.mmfs.add(mmf); mmf.order(ByteOrder.nativeOrder()); for (int i=0; i<nbuffers; i++) { Unit ii = new Unit(); mmf.position(i * Unit.SIZE); mmf.limit(mmf.position() + Unit.SIZE); ii.buf = mmf.slice(); ii.addr = UberUtil.getAddress(ii.buf); this.units.add(ii); } this.ch.close(); this.raf.close(); } }
Example 6
Source File: InputReader.java From perf-workshop with Apache License 2.0 | 5 votes |
private void processSingleFile(final MappedByteBuffer mappedFile, final boolean isLastIteration) { for (int recordIndex = 0; recordIndex < commandLineArgs.getNumberOfRecords(); recordIndex++) { if (!messageSink.hasAvailableCapacity(1)) { throw new RuntimeException("RingBuffer full!"); } final long sequence = messageSink.next(); try { final Packet packet = messageSink.get(sequence); packet.reset(); packet.setSequenceInFile(recordIndex); packet.setSequence(sequence); final boolean isLastInFile = recordIndex == commandLineArgs.getNumberOfRecords() - 1; packet.setLastInFile(isLastInFile); packet.setReceivedNanoTime(nanoTimer.nanoTime()); packet.setLastInStream(isLastInFile && isLastIteration); mappedFile.position(recordIndex * commandLineArgs.getRecordLength()); mappedFile.limit(mappedFile.position() + commandLineArgs.getRecordLength()); packet.getPayload().put(mappedFile); } finally { messageSink.publish(sequence); } introduceMessagePublishDelay(); } }
Example 7
Source File: DylibStubContentsScrubber.java From buck with Apache License 2.0 | 5 votes |
private static void resetSymbolAddressesInExportInfo( MappedByteBuffer machoBuffer, MachoDyldInfoCommand dyldInfoCommand) { machoBuffer.position(dyldInfoCommand.getExportInfoOffset()); machoBuffer.limit(dyldInfoCommand.getExportInfoOffset() + dyldInfoCommand.getExportInfoSize()); ByteBuffer exportInfoBuffer = machoBuffer.slice(); Optional<MachoExportTrieNode> tree = MachoExportTrieReader.read(exportInfoBuffer); if (tree.isPresent()) { resetSymbolAddressesInTree(tree.get()); // We want to reset all addresses to zero and write out a new tree rather than just setting // all existing addresses to zero: that's because symbol offset sizes depends on the offsets // themselves, so we want to make the scrubbed dylib stub be independent from those addresses. // Otherwise, it's possible that the dylib stub will be logically the same (i.e., all // addresses zero) but different on disk due to zero being expressed using different number // of ULEB128 bytes. exportInfoBuffer.rewind(); MachoExportTrieWriter.write(tree.get(), exportInfoBuffer); // As we set all addresses to 0x0, that means the total size of the trie will _never_ be // larger than the initial trie. Consequently, we do not need to adjust segment sizes and // just need to zero out any remaining parts in the export section. while (exportInfoBuffer.position() < exportInfoBuffer.limit()) { exportInfoBuffer.put((byte) 0x00); } } }
Example 8
Source File: TextFiles.java From Java-Coding-Problems with MIT License | 4 votes |
public static long countOccurrencesV5(Path path, String text) throws IOException { if (path == null || text == null) { throw new IllegalArgumentException("Path/text cannot be null"); } if (text.isBlank()) { return 0; } final byte[] texttofind = text.getBytes(StandardCharsets.UTF_8); long count = 0; try (FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.READ)) { long position = 0; long length = fileChannel.size(); while (position < length) { long remaining = length - position; long bytestomap = (long) Math.min(MAP_SIZE, remaining); MappedByteBuffer mbBuffer = fileChannel.map(MapMode.READ_ONLY, position, bytestomap); long limit = mbBuffer.limit(); long lastSpace = -1; long firstChar = -1; while (mbBuffer.hasRemaining()) { boolean isFirstChar = false; while (firstChar != 0 && mbBuffer.hasRemaining()) { byte currentByte = mbBuffer.get(); if (Character.isWhitespace((char) currentByte)) { lastSpace = mbBuffer.position(); } if (texttofind[0] == currentByte) { isFirstChar = true; break; } } if (isFirstChar) { boolean isRestOfChars = true; int j; for (j = 1; j < texttofind.length; j++) { if (!mbBuffer.hasRemaining() || texttofind[j] != mbBuffer.get()) { isRestOfChars = false; break; } } if (isRestOfChars) { count++; lastSpace = -1; } firstChar = -1; } } if (lastSpace > -1) { position = position - (limit - lastSpace); } position += bytestomap; } } return count; }