类java.nio.MappedByteBuffer源码实例Demo

下面列出了怎么用java.nio.MappedByteBuffer的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: sasi   文件: MappedBuffer.java
@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
    }
}
 
源代码2 项目: bboxdb   文件: TestUnsafeMemoryHandler.java
/**
 * 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());
}
 
源代码3 项目: antsdb   文件: AntiCrashCrimeScene.java
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
    }
}
 
源代码4 项目: SoftwarePilot   文件: AutoSelfie.java
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];
}
 
源代码5 项目: jelectrum   文件: LongMappedBuffer.java
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");
}
 
源代码6 项目: aeron   文件: CncFileReader.java
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));
}
 
源代码7 项目: openjdk-8   文件: MappedReadBuffer.java
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);
}
 
源代码8 项目: consulo   文件: MappedBufferWrapper.java
@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);
      }
    }
  }
}
 
源代码9 项目: j2objc   文件: BufferTest.java
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();
}
 
源代码10 项目: proxyee-down   文件: LargeMappedByteBuffer.java
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);
  }
}
 
源代码11 项目: basic-tools   文件: TestReadFile.java
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));
    }


}
 
源代码12 项目: aeron   文件: SamplesUtil.java
/**
 * 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));
}
 
源代码13 项目: ph-commons   文件: FileChannelHelper.java
@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;
  }
}
 
源代码14 项目: j2objc   文件: FileChannelTest.java
/**
 * @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;
    }
}
 
源代码15 项目: monsoon   文件: XdrEncodingFileWriterTest.java
@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);
}
 
源代码16 项目: aeron   文件: ArchiveTool.java
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);
}
 
源代码17 项目: CrawlerForReader   文件: FileIOUtils.java
/**
 * 将字节数组写入文件
 *
 * @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);
    }
}
 
源代码18 项目: offheap-store   文件: MappedPageSource.java
@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;
  }
}
 
源代码19 项目: buck   文件: DylibStubContentsScrubber.java
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
  }
}
 
@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);
    }
}
 
源代码21 项目: fqueue   文件: MemoryMappedBlockStore.java
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);
}
 
源代码22 项目: akarnokd-misc   文件: EditorIDScanner.java
public static void main(String[] args) throws Exception {
    File file = new File(
            "c:\\Program Files (x86)\\Bethesda.net Launcher\\games\\Fallout76\\Data\\SeventySix.esm");

    /*
     * char[4] = 'EDID'
     * uint16 = length
     * uint8[length] = characters
     */
    int i = 1;
    try (PrintWriter out = new PrintWriter(new FileWriter("editorids.txt"))) {
        try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {

            MappedByteBuffer mbb = raf.getChannel().map(MapMode.READ_ONLY, 0, raf.length());
            
            
            while (mbb.remaining() > 0) {
                if (mbb.get() == 'E') {
                    if (mbb.get() == 'D') {
                        if (mbb.get() == 'I') {
                            if (mbb.get() == 'D') {
                                int len = Short.reverseBytes(mbb.getShort()) & 0xFFFF;
                                byte[] data = new byte[len];
                                mbb.get(data);
                                out.println(new String(data, StandardCharsets.ISO_8859_1));
                                if (i % 100 == 0) {
                                    System.out.println("Records so far: " + i);
                                }
                                i++;
                            }
                        }
                    }
                }
            }
        }
    }
}
 
源代码23 项目: diozero   文件: SysFsPerfTest.java
private static void testMmapRead(int gpio, int iterations) throws IOException {
	try (FileChannel fc = FileChannel.open(Paths.get("/sys/class/gpio/gpio" + gpio + "/value"),
			StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.SYNC)) {
		MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 0, 1);
		long start = System.currentTimeMillis();
		for (int i=0; i<iterations; i++) {
			mbb.get(0);
		}
		long duration = System.currentTimeMillis() - start;
		Logger.info("mmap read: {0.000} ms per iteration", Double.valueOf(duration*1000 / (double) iterations));
	}
}
 
源代码24 项目: netbeans   文件: FastMatcher.java
/**
 * 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) {
    }
}
 
源代码25 项目: annoy-java   文件: ANNIndex.java
@Override
public void getNodeVector(final long nodeOffset, float[] v) {
  MappedByteBuffer nodeBuf = buffers[(int) (nodeOffset / BLOCK_SIZE)];
  int offset = (int) ((nodeOffset % BLOCK_SIZE) + K_NODE_HEADER_STYLE);
  for (int i = 0; i < DIMENSION; i++) {
    v[i] = nodeBuf.getFloat(offset + i * FLOAT_SIZE);
  }
}
 
源代码26 项目: imhotep   文件: FileSerializationBenchmark.java
@Override
public void serialize(int[] a, File file) throws IOException {
    MappedByteBuffer buffer = Files.map(file, FileChannel.MapMode.READ_WRITE, a.length * 4);
    buffer.order(ByteOrder.BIG_ENDIAN);
    IntBuffer intBuffer = buffer.asIntBuffer();
    for (int i = 0; i < a.length; ++i) {
        intBuffer.put(i, a[i]);
    }
}
 
源代码27 项目: sakai   文件: EmailTemplateServiceImpl.java
private static String readFile(String path) throws IOException {
	try (FileInputStream stream = new FileInputStream(new File(path))) {
		FileChannel fc = stream.getChannel();
		MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
		/* Instead of using default, pass in a decoder. */
		return Charset.defaultCharset().decode(bb).toString();
	}
}
 
/**
 * Memory-map the model file in Assets.
 */
public static MappedByteBuffer loadModelFile(Context context, String modelFile)
        throws IOException {
    AssetFileDescriptor fileDescriptor = context.getAssets().openFd(modelFile);
    FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
    FileChannel fileChannel = inputStream.getChannel();
    long startOffset = fileDescriptor.getStartOffset();
    long declaredLength = fileDescriptor.getDeclaredLength();
    return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
 
源代码29 项目: jelectrum   文件: Slopbucket.java
protected void writeLong(long position, long value)
{
  int file = (int) (position / SEGMENT_FILE_SIZE);
  int offset_in_file = (int) (position % SEGMENT_FILE_SIZE);
  MappedByteBuffer mbb = getBufferMap(file);
  synchronized(mbb)
  {
    mbb.position(offset_in_file);
    mbb.putLong(value);
  }
}
 
源代码30 项目: jelectrum   文件: Slopbucket.java
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;
}