java.nio.MappedByteBuffer Java Examples
The following examples show how to use
java.nio.MappedByteBuffer.
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: DylibStubContentsScrubber.java From buck with Apache License 2.0 | 6 votes |
private static void resetSymbolAddressesInSymbolTable( MappedByteBuffer machoBuffer, MachoSymTabCommand symTabCommand) { machoBuffer.position(symTabCommand.getSymbolTableOffset()); for (int i = 0; i < symTabCommand.getNumberOfSymbolTableEntries(); ++i) { // struct nlist_64 { // union { // uint32_t n_strx; // index into the string table // } n_un; // uint8_t n_type; // type flag, see below // uint8_t n_sect; // section number or NO_SECT // uint16_t n_desc; // see <mach-o/stab.h> // uint64_t n_value; // value of this symbol (or stab offset) // }; ObjectFileScrubbers.getLittleEndianInt(machoBuffer); // n_strx machoBuffer.get(); // n_type machoBuffer.get(); // n_sect ObjectFileScrubbers.getLittleEndianShort(machoBuffer); // n_desc ObjectFileScrubbers.putLittleEndianLong(machoBuffer, 0x0); // n_value } }
Example #2
Source File: AutoSelfie.java From SoftwarePilot with MIT License | 6 votes |
byte[] readImg(){ try { //BufferedImage origImage = ImageIO.read(imgPath); //ByteArrayOutputStream baos = new ByteArrayOutputStream(); //ImageIO.write(origImage, "jpg", baos); //return baos.toByteArray(); File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG"); //File file = new File("../tmp/pictmp.jpg"); FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel(); MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); pic = new byte[buffer.capacity()]; while(buffer.hasRemaining()){ int remaining = pic.length; if(buffer.remaining() < remaining){ remaining = buffer.remaining(); } buffer.get(pic, 0, remaining); } return pic; } catch(Exception e){ e.printStackTrace(); } return new byte[0]; }
Example #3
Source File: LongMappedBuffer.java From jelectrum with MIT License | 6 votes |
public void putBytes(long position, byte[] buff) { long t1 = System.nanoTime(); //Assert.assertTrue(position >= 0); //Assert.assertTrue(position + buff.length <= total_size); int to_write=buff.length; int start_file = (int) (position / MAP_SIZE); int start_offset = (int) (position % MAP_SIZE); MappedByteBuffer map = map_list.get(start_file); map.position(start_offset); int len = Math.min(to_write, (int) (MAP_SIZE - start_offset)); map.put(buff, 0, len); if (len < to_write) { map = map_list.get(start_file + 1); map.position(0); map.put(buff, len, to_write - len); } TimeRecord.record(t1, "long_map_put_bytes"); }
Example #4
Source File: TestUnsafeMemoryHandler.java From bboxdb with Apache License 2.0 | 6 votes |
/** * Test the unmapping of memory mapped files (if available in JVM) * @throws Exception */ @Test(timeout=60000) public void testUnsafeMemoryHandler() throws Exception { if(! UnsafeMemoryHelper.isDirectMemoryUnmapperAvailable()) { return; } final long oldBytes = UnsafeMemoryHelper.getMappedBytes(); final long oldSegments = UnsafeMemoryHelper.getMappedSegments(); UnsafeMemoryHelper.unmapMemory(null); final RandomAccessFile randomAccessFile = new RandomAccessFile("/tmp/mapfile", "rw"); final MappedByteBuffer mappedBytes = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 100); Assert.assertEquals(oldBytes + 100, UnsafeMemoryHelper.getMappedBytes()); Assert.assertEquals(oldSegments + 1, UnsafeMemoryHelper.getMappedSegments()); randomAccessFile.close(); UnsafeMemoryHelper.unmapMemory(mappedBytes); Assert.assertEquals(oldBytes, UnsafeMemoryHelper.getMappedBytes()); Assert.assertEquals(oldSegments, UnsafeMemoryHelper.getMappedSegments()); }
Example #5
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 #6
Source File: MappedBuffer.java From sasi with Apache License 2.0 | 6 votes |
@Override public void close() { if (!FileUtils.isCleanerAvailable()) return; /* * Try forcing the unmapping of pages using undocumented unsafe sun APIs. * If this fails (non Sun JVM), we'll have to wait for the GC to finalize the mapping. * If this works and a thread tries to access any page, hell will unleash on earth. */ try { for (MappedByteBuffer segment : pages) FileUtils.clean(segment); } catch (Exception e) { // This is not supposed to happen } }
Example #7
Source File: CncFileReader.java From aeron with Apache License 2.0 | 6 votes |
private CncFileReader(final MappedByteBuffer cncByteBuffer) { this.cncByteBuffer = cncByteBuffer; final DirectBuffer cncMetaDataBuffer = createMetaDataBuffer(cncByteBuffer); final int cncVersion = cncMetaDataBuffer.getInt(cncVersionOffset(0)); try { checkVersion(cncVersion); } catch (final AeronException e) { IoUtil.unmap(cncByteBuffer); throw e; } this.cncVersion = cncVersion; this.cncSemanticVersion = SemanticVersion.toString(cncVersion); this.toDriverBuffer = CncFileDescriptor.createToDriverBuffer(cncByteBuffer, cncMetaDataBuffer); this.countersReader = new CountersReader( createCountersMetaDataBuffer(cncByteBuffer, cncMetaDataBuffer), createCountersValuesBuffer(cncByteBuffer, cncMetaDataBuffer)); }
Example #8
Source File: MappedReadBuffer.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
static ReadBuffer create(RandomAccessFile file) throws IOException { FileChannel ch = file.getChannel(); long size = ch.size(); // if file size is more than 2 GB and when file mapping is // configured (default), use mapped file reader if (canUseFileMap() && (size <= Integer.MAX_VALUE)) { MappedByteBuffer buf; try { buf = ch.map(FileChannel.MapMode.READ_ONLY, 0, size); ch.close(); return new MappedReadBuffer(buf); } catch (IOException exp) { exp.printStackTrace(); System.err.println("File mapping failed, will use direct read"); // fall through } } // else fall through return new FileReadBuffer(file); }
Example #9
Source File: MappedBufferWrapper.java From consulo with Apache License 2.0 | 6 votes |
@Override public void flush() { MappedByteBuffer buffer = myBuffer; if (buffer != null && isDirty()) { for (int i = 0; i < MAX_FORCE_ATTEMPTS; i++) { try { buffer.force(); myDirty = false; break; } catch (Throwable e) { Logger.getInstance(MappedBufferWrapper.class).info(e); TimeoutUtil.sleep(10); } } } }
Example #10
Source File: BufferTest.java From j2objc with Apache License 2.0 | 6 votes |
public void testBug53637() throws Exception { MappedByteBuffer mapped = (MappedByteBuffer) allocateMapped(1); mapped.get(); mapped.rewind(); mapped.get(); mapped.rewind(); mapped.mark(); mapped.get(); mapped.reset(); mapped.get(); mapped.rewind(); mapped.get(); mapped.clear(); mapped.get(); mapped.rewind(); mapped.get(); mapped.flip(); mapped.get(); }
Example #11
Source File: TestReadFile.java From basic-tools with MIT License | 6 votes |
public static void main(String[] args) throws IOException { String smallFilePath = "/Users/renhongqiang/Downloads/work-doc/2000W/demo.txt"; File file = new File(smallFilePath); RandomAccessFile rAccessFile = new RandomAccessFile(file, "r"); MappedByteBuffer mapBuffer = rAccessFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length()); int bufferSize = 10; byte[] buffer = new byte[10]; for (int offset = 0; offset < file.length(); offset += bufferSize) { int readLength; if (offset + bufferSize <= file.length()) { readLength = bufferSize; } else { readLength = (int) (file.length() - offset); } mapBuffer.get(buffer, 0, readLength); System.out.println(new String(buffer, StandardCharsets.UTF_8)); } }
Example #12
Source File: FileChannelHelper.java From ph-commons with Apache License 2.0 | 6 votes |
@Nullable private static OutputStream _getMappedOutputStream (@Nonnull @WillNotClose final FileChannel aChannel, @Nonnull final File aFile) { try { // Maximum is Integer.MAX_VALUE even if a long is taken! final MappedByteBuffer aBuffer = aChannel.map (MapMode.READ_WRITE, 0, Integer.MAX_VALUE); if (LOGGER.isInfoEnabled ()) LOGGER.info ("Created memory mapped output stream for " + aFile); return new ByteBufferOutputStream (aBuffer, false); } catch (final IOException ex) { LOGGER.warn ("Failed to create memory mapped output stream for " + aFile, ex); return null; } }
Example #13
Source File: MappedPageSource.java From offheap-store with Apache License 2.0 | 6 votes |
@Override public synchronized MappedPage allocate(int size, boolean thief, boolean victim, OffHeapStorageArea owner) { Long address = allocateRegion(size); if (address == null) { return null; } try { MappedByteBuffer buffer = channel.map(MapMode.READ_WRITE, address, size); MappedPage page = new MappedPage(buffer); pages.put(page, address); return page; } catch (IOException e) { freeRegion(address); LOGGER.warn("Mapping a new file section failed", e); return null; } }
Example #14
Source File: FileIOUtils.java From CrawlerForReader with Apache License 2.0 | 6 votes |
/** * 将字节数组写入文件 * * @param file 文件 * @param bytes 字节数组 * @param append 是否追加在文件末 * @param isForce 是否写入文件 * @return {@code true}: 写入成功<br>{@code false}: 写入失败 */ public static boolean writeFileFromBytesByMap(final File file, final byte[] bytes, final boolean append, final boolean isForce) { if (bytes == null || !createOrExistsFile(file)) return false; FileChannel fc = null; try { fc = new FileOutputStream(file, append).getChannel(); MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, fc.size(), bytes.length); mbb.put(bytes); if (isForce) mbb.force(); return true; } catch (IOException e) { e.printStackTrace(); return false; } finally { CloseUtils.closeIO(fc); } }
Example #15
Source File: ArchiveTool.java From aeron with Apache License 2.0 | 6 votes |
private static void printErrors(final PrintStream out, final ArchiveMarkFile markFile) { out.println("Archive error log:"); CommonContext.printErrorLog(markFile.errorBuffer(), out); final MarkFileHeaderDecoder decoder = markFile.decoder(); decoder.skipControlChannel(); decoder.skipLocalControlChannel(); decoder.skipEventsChannel(); final String aeronDirectory = decoder.aeronDirectory(); out.println(); out.println("Aeron driver error log (directory: " + aeronDirectory + "):"); final File cncFile = new File(aeronDirectory, CncFileDescriptor.CNC_FILE); final MappedByteBuffer cncByteBuffer = IoUtil.mapExistingFile(cncFile, FileChannel.MapMode.READ_ONLY, "cnc"); final DirectBuffer cncMetaDataBuffer = CncFileDescriptor.createMetaDataBuffer(cncByteBuffer); final int cncVersion = cncMetaDataBuffer.getInt(CncFileDescriptor.cncVersionOffset(0)); CncFileDescriptor.checkVersion(cncVersion); CommonContext.printErrorLog(CncFileDescriptor.createErrorLogBuffer(cncByteBuffer, cncMetaDataBuffer), out); }
Example #16
Source File: XdrEncodingFileWriterTest.java From monsoon with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void writeBytes() throws Exception { byte output[] = new byte[bytes.length]; try (FileChannel fd = FileChannel.open(file.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE)) { try (XdrEncodingFileWriter writer = new XdrEncodingFileWriter(new FileChannelWriter(fd, 0))) { writer.beginEncoding(); writer.xdrEncodeOpaque(bytes, bytes.length); writer.endEncoding(); } MappedByteBuffer map = fd.map(FileChannel.MapMode.READ_ONLY, 0, fd.size()); map.order(ByteOrder.BIG_ENDIAN); map.get(output); assertFalse(map.hasRemaining()); } assertArrayEquals(bytes, output); }
Example #17
Source File: FileChannelTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * @tests java.nio.channels.FileChannel#map(MapMode,long,long) */ public void test_map_ReadWrite() throws IOException { MappedByteBuffer mapped = null; writeDataToFile(fileOfReadWriteFileChannel); mapped = readWriteFileChannel.map(MapMode.READ_WRITE, 0, CONTENT .length()); // put something will change its channel ByteBuffer returnByPut = mapped.put(TEST_BYTES); assertSame(returnByPut, mapped); String checkString = "test" + CONTENT.substring(4); ByteBuffer checkBuffer = ByteBuffer.allocate(CONTENT_LENGTH); mapped.force(); readWriteFileChannel.position(0); readWriteFileChannel.read(checkBuffer); assertEquals(checkString, new String(checkBuffer.array(), "iso8859-1")); try { mapped.put(("test" + CONTENT).getBytes("iso8859-1")); fail("should throw BufferOverflowException."); } catch (BufferOverflowException ex) { // expected; } }
Example #18
Source File: SamplesUtil.java From aeron with Apache License 2.0 | 6 votes |
/** * Map an existing CnC file. * * @param cncFileVersion to set as value of file. * @return the {@link CountersReader} over the CnC file. */ public static CountersReader mapCounters(final MutableInteger cncFileVersion) { final File cncFile = CommonContext.newDefaultCncFile(); System.out.println("Command `n Control file " + cncFile); final MappedByteBuffer cncByteBuffer = mapExistingFileReadOnly(cncFile); final DirectBuffer cncMetaData = createMetaDataBuffer(cncByteBuffer); final int cncVersion = cncMetaData.getInt(cncVersionOffset(0)); cncFileVersion.set(cncVersion); checkVersion(cncVersion); return new CountersReader( createCountersMetaDataBuffer(cncByteBuffer, cncMetaData), createCountersValuesBuffer(cncByteBuffer, cncMetaData)); }
Example #19
Source File: LargeMappedByteBuffer.java From proxyee-down with Apache License 2.0 | 6 votes |
public final void put(ByteBuffer byteBuffer) throws IOException { try { int index = getIndex(); long length = byteBuffer.limit() - byteBuffer.position(); this.position += length; MappedByteBuffer mappedBuffer = bufferList.get(index); if (mappedBuffer.remaining() < length) { byte[] temp = new byte[mappedBuffer.remaining()]; byteBuffer.get(temp); bufferList.get(index).put(temp); bufferList.get(index + 1).put(byteBuffer); } else { bufferList.get(index).put(byteBuffer); } } catch (Exception e) { throw new IOException( "LargeMappedByteBuffer put rawPosition-" + rawPosition + "\tposition-" + position + "\tsize-" + size, e); } }
Example #20
Source File: FastMatcher.java From netbeans with Apache License 2.0 | 5 votes |
/** * Unmap mapped buffer. */ private void unmap(MappedByteBuffer buffer) { try { Method getCleanerMethod = buffer.getClass().getMethod( "cleaner"); //NOI18N getCleanerMethod.setAccessible(true); // sun.misc.Cleaner Object cleaner = getCleanerMethod.invoke(buffer); cleaner.getClass().getMethod("clean").invoke(cleaner); } catch (Exception e) { } }
Example #21
Source File: ApkSignatureSchemeV2Verifier.java From titan-hotfix with Apache License 2.0 | 5 votes |
@Override public void feedIntoMessageDigests( MessageDigest[] mds, long offset, int size) throws IOException { long filePosition = mFilePosition + offset; MappedByteBuffer buf = mChannel.map(FileChannel.MapMode.READ_ONLY, filePosition, size); for (MessageDigest md : mds) { buf.position(0); md.update(buf); } }
Example #22
Source File: Slopbucket.java From jelectrum with MIT License | 5 votes |
protected Map<String, Integer> loadTroughMap() { TreeMap<String,Integer> map = new TreeMap<>(); MappedByteBuffer mbb = getBufferMap(0); synchronized(mbb) { for(int i=0; i<MAX_TROUGHS; i++) { mbb.position( (int)(LOCATION_TROUGH_TABLE_START + (8 + MAX_TROUGH_NAME_LEN) * i)); long ptr = mbb.getLong(); byte[] name = new byte[MAX_TROUGH_NAME_LEN]; mbb.get(name); int len =0; for(int j=0; (j<MAX_TROUGH_NAME_LEN) && (name[j] != 0); j++) { len++; } if (len > 0) { String name_str = new String(name, 0, len); map.put(name_str, i); } else { map.put("__FREE", i); } } } return map; }
Example #23
Source File: Mode.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private static void testWritable() throws IOException { FileOutputStream is = new FileOutputStream(testFile); FileChannel channel = is.getChannel(); try { MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 0, 8); throw new RuntimeException("Exception expected, none thrown"); } catch (NonReadableChannelException e) { // correct result } is.close(); }
Example #24
Source File: FBEngine.java From WarpPI with Apache License 2.0 | 5 votes |
@Override public void create(final Runnable onInitialized) { onResize = BehaviorSubject.create(new Integer[] { FBEngine.SIZE[0], FBEngine.SIZE[1] }); realFb = jni.retrieveBuffer(); final long fbLen = realFb.getLength(); fb = (MappedByteBuffer) ByteBuffer.allocateDirect((int) fbLen); r = new FBRenderer(this, fb); initialized = true; if (onInitialized != null) onInitialized.run(); }
Example #25
Source File: MappedLevelControllerRepo.java From vespa with Apache License 2.0 | 5 votes |
MappedLevelControllerRepo(MappedByteBuffer mapBuf, int controlFileHeaderLength, int numLevels, String logControlFilename) { this.mapBuf = mapBuf; this.controlFileHeaderLength = controlFileHeaderLength; this.numLevels = numLevels; this.logControlFilename = logControlFilename; buildMap(); }
Example #26
Source File: LargeMappedByteBuffer.java From proxyee-down with Apache License 2.0 | 5 votes |
@Override public void close() throws IOException { try { Class<?> clazz = Class.forName("sun.nio.ch.FileChannelImpl"); Method m = clazz.getDeclaredMethod("unmap", MappedByteBuffer.class); m.setAccessible(true); for (MappedByteBuffer mappedBuffer : bufferList) { m.invoke(clazz, mappedBuffer); } } catch (Exception e) { throw new IOException("LargeMappedByteBuffer close", e); } }
Example #27
Source File: EncryptUtil.java From hdw-dubbo with Apache License 2.0 | 5 votes |
/*** * 获取文件SHA1值 * * @return String 适用于上G大的文件 */ public static String sha1(File file) { try { MessageDigest messagedigest = MessageDigest.getInstance("SHA-1"); FileInputStream in = new FileInputStream(file); FileChannel ch = in.getChannel(); MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); messagedigest.update(byteBuffer); return bufferToHex(messagedigest.digest()); } catch (Exception e) { return null; } }
Example #28
Source File: MemoryMappedBlockStore.java From fqueue with Apache License 2.0 | 5 votes |
private MappedByteBuffer getMemoryMappedFileStorage(long maxBytes, String fileName) throws IOException { // open the file for read-write this.fileName = fileName; fileStorage = new RandomAccessFile(fileName, "rw"); fileStorage.seek(maxBytes); fileStorage.getChannel().map(PRIVATE, 0, maxBytes); return fileStorage.getChannel().map(PRIVATE, 0, maxBytes); }
Example #29
Source File: AssetModelLoader.java From PHONK with GNU General Public License v3.0 | 5 votes |
protected MappedByteBuffer loadMappedFile(String filePath) throws IOException { AssetFileDescriptor fileDescriptor = assetManager.openFd(this.directoryName + "/" + filePath); FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor()); FileChannel fileChannel = inputStream.getChannel(); long startOffset = fileDescriptor.getStartOffset(); long declaredLength = fileDescriptor.getDeclaredLength(); return fileChannel.map(MapMode.READ_ONLY, startOffset, declaredLength); }
Example #30
Source File: GDataObject.java From hortonmachine with GNU General Public License v3.0 | 5 votes |
/** * */ public DataStore initEnumerator() { try { if (dataType == POINT_DATA) { if (backing == USE_BACKING_FILE) { readBuffer = null; readBuffer = ((MappedByteBuffer) (backingFile.getChannel().map( FileChannel.MapMode.READ_ONLY, 0, backingFile.length()))).force() .load(); } else if (backing == USE_BACKING_MEMORY) readBuffer.rewind(); offset = 0; bytesToRead = columnwidth; return this; } else if (dataType == VECTOR_DATA) { if (backing == USE_BACKING_FILE) { readBuffer = null; readBuffer = ((MappedByteBuffer) (backingFile.getChannel().map( FileChannel.MapMode.READ_ONLY, 0, backingFile.length()))).force() .load(); } else if (backing == USE_BACKING_MEMORY) readBuffer.rewind(); offset = 0; bytesToRead = (readBuffer.getInt(0) * 8) + 4; return this; } else if (dataType == RASTER_DATA) { if (backing == USE_BACKING_MEMORY) readBuffer.rewind(); return this; } } catch (IOException ioe) { } return null; }