Java Code Examples for java.nio.MappedByteBuffer#put()

The following examples show how to use java.nio.MappedByteBuffer#put() . 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: LongMappedBuffer.java    From jelectrum with MIT License 6 votes vote down vote up
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 2
Source File: MapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Maps blah file with a random offset and checks to see if data
 * written out to the file can be read back in
 */
private static void testWrite() throws Exception {
    StringBuilder sb = new StringBuilder();
    sb.setLength(4);

    for (int x=0; x<1000; x++) {
        try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {
            FileChannel fc = raf.getChannel();

            long offset = generator.nextInt(1000);
            MappedByteBuffer b = fc.map(MapMode.READ_WRITE,
                                        offset, 100);

            for (int i=0; i<4; i++) {
                b.put(i, (byte)('0' + i));
            }

            for (int i=0; i<4; i++) {
                byte aByte = b.get(i);
                sb.setCharAt(i, (char)aByte);
            }
            if (!sb.toString().equals("0123"))
                throw new Exception("Write test failed");
        }
    }
}
 
Example 3
Source File: FileIOUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * 将字节数组写入文件
 *
 * @param file    文件
 * @param bytes   字节数组
 * @param append  是否追加在文件末
 * @param isForce 是否写入文件
 * @return {@code true}: 写入成功<br>{@code false}: 写入失败
 */
public static boolean writeFileFromBytesByMap(File file, final byte[] bytes, boolean append, boolean isForce) {
    if (bytes == null || !FileUtils.createOrExistsFile(file)) return false;
    if (!append && !FileUtils.createFileByDeleteOldFile(file)) return false;
    FileChannel fc = null;
    try {
        fc = new RandomAccessFile(file, "rw").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 4
Source File: MapTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Maps blah file with a random offset and checks to see if data
 * written out to the file can be read back in
 */
private static void testWrite() throws Exception {
    StringBuilder sb = new StringBuilder();
    sb.setLength(4);

    for (int x=0; x<1000; x++) {
        try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {
            FileChannel fc = raf.getChannel();

            long offset = generator.nextInt(1000);
            MappedByteBuffer b = fc.map(MapMode.READ_WRITE,
                                        offset, 100);

            for (int i=0; i<4; i++) {
                b.put(i, (byte)('0' + i));
            }

            for (int i=0; i<4; i++) {
                byte aByte = b.get(i);
                sb.setCharAt(i, (char)aByte);
            }
            if (!sb.toString().equals("0123"))
                throw new Exception("Write test failed");
        }
    }
}
 
Example 5
Source File: GenericCopyUtil.java    From PowerFileExplorer with GNU General Public License v3.0 6 votes vote down vote up
private boolean copyFile(BufferedInputStream bufferedInputStream, FileChannel outChannel)
	throws IOException {

        MappedByteBuffer byteBuffer = outChannel.map(FileChannel.MapMode.READ_WRITE, 0,
													 mSourceFile.size);
        int count = 0;
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
		while ((count = bufferedInputStream.read(buffer)) != -1) {
			if (progressHandler.isCancelled) {
				outChannel.close();
				return false;
			}
//            count = bufferedInputStream.read(buffer);
//            if (count != -1) {
			byteBuffer.put(buffer, 0, count);
			ServiceWatcherUtil.POSITION += count;
//            }
        }
		return true;
    }
 
Example 6
Source File: FileIOUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 通过 MappedByteBuffer 把字节流写入文件
 * @param file    文件
 * @param bytes   byte[]
 * @param append  是否追加到结尾
 * @param isForce 是否强制写入
 * @return {@code true} success, {@code false} fail
 */
public static boolean writeFileFromBytesByMap(final File file, final byte[] bytes, final boolean append, final boolean isForce) {
    if (bytes == null || !FileUtils.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) {
        JCLogUtils.eTag(TAG, e, "writeFileFromBytesByMap");
        return false;
    } finally {
        CloseUtils.closeIOQuietly(fc);
    }
}
 
Example 7
Source File: LongMappedBufferMany.java    From jelectrum with MIT License 6 votes vote down vote up
public synchronized 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 8
Source File: FileRunner.java    From fqueue with Apache License 2.0 6 votes vote down vote up
private boolean create(String path) throws IOException {
    File file = new File(path);
    if (file.exists() == false) {
        if (file.createNewFile() == false) {
            return false;
        }
        RandomAccessFile raFile = new RandomAccessFile(file, "rwd");
        FileChannel fc = raFile.getChannel();
        MappedByteBuffer mappedByteBuffer = fc.map(MapMode.READ_WRITE, 0, this.fileLimitLength);
        mappedByteBuffer.put(LogEntity.MAGIC.getBytes());
        mappedByteBuffer.putInt(1);// 8 version
        mappedByteBuffer.putInt(-1);// 12next fileindex
        mappedByteBuffer.putInt(-2);// 16
        mappedByteBuffer.force();
        MappedByteBufferUtil.clean(mappedByteBuffer);
        fc.close();
        raFile.close();
        return true;
    } else {
        return false;
    }
}
 
Example 9
Source File: IoUtil.java    From agrona with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new file, and optionally fill with 0s, and return a {@link java.nio.MappedByteBuffer} for the file.
 * <p>
 * The file itself will be closed, but the mapping will persist.
 *
 * @param location      of the file to create and map.
 * @param length        of the file to create and map.
 * @param fillWithZeros to force allocation..
 * @return {@link java.nio.MappedByteBuffer} for the file.
 */
public static MappedByteBuffer mapNewFile(final File location, final long length, final boolean fillWithZeros)
{
    MappedByteBuffer mappedByteBuffer = null;
    try (FileChannel channel = FileChannel.open(location.toPath(), CREATE_NEW, READ, WRITE))
    {
        mappedByteBuffer = channel.map(READ_WRITE, 0, length);
        if (fillWithZeros)
        {
            int pos = 0;
            while (pos < length)
            {
                mappedByteBuffer.put(pos, (byte)0);
                pos += BLOCK_SIZE;
            }
        }
    }
    catch (final IOException ex)
    {
        LangUtil.rethrowUnchecked(ex);
    }

    return mappedByteBuffer;
}
 
Example 10
Source File: FileRunner.java    From fqueue with Apache License 2.0 6 votes vote down vote up
private boolean create(String path) throws IOException {
    File file = new File(path);
    if (file.exists() == false) {
        if (file.createNewFile() == false) {
            return false;
        }
        RandomAccessFile raFile = new RandomAccessFile(file, "rwd");
        FileChannel fc = raFile.getChannel();
        MappedByteBuffer mappedByteBuffer = fc.map(MapMode.READ_WRITE, 0, this.fileLimitLength);
        mappedByteBuffer.put(LogEntity.MAGIC.getBytes());
        mappedByteBuffer.putInt(1);// 8 version
        mappedByteBuffer.putInt(-1);// 12next fileindex
        mappedByteBuffer.putInt(-2);// 16
        mappedByteBuffer.force();
        MappedByteBufferUtil.clean(mappedByteBuffer);
        fc.close();
        raFile.close();
        return true;
    } else {
        return false;
    }
}
 
Example 11
Source File: SpillMap.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private void flushBuffer() throws BufferOverflowException {
    if (pagedIn) {
        MappedByteBuffer buffer;
        // Only flush if page was changed
        if (dirtyPage) {
            Collection<byte[]> values = pageMap.values();
            buffer = spillFile.getPage(pageIndex);
            buffer.clear();
            // number of elements
            buffer.putInt(values.size());
            for (byte[] value : values) {
                // element length
                buffer.putInt(value.length);
                // element
                buffer.put(value, 0, value.length);
            }
        }
        buffer = null;
        // Reset page stats
        pageMap.clear();
        totalResultSize = 0;
    }
    pagedIn = false;
    dirtyPage = false;
}
 
Example 12
Source File: MapTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Maps blah file with a random offset and checks to see if data
 * written out to the file can be read back in
 */
private static void testWrite() throws Exception {
    StringBuilder sb = new StringBuilder();
    sb.setLength(4);

    for (int x=0; x<1000; x++) {
        try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {
            FileChannel fc = raf.getChannel();

            long offset = generator.nextInt(1000);
            MappedByteBuffer b = fc.map(MapMode.READ_WRITE,
                                        offset, 100);

            for (int i=0; i<4; i++) {
                b.put(i, (byte)('0' + i));
            }

            for (int i=0; i<4; i++) {
                byte aByte = b.get(i);
                sb.setCharAt(i, (char)aByte);
            }
            if (!sb.toString().equals("0123"))
                throw new Exception("Write test failed");
        }
    }
}
 
Example 13
Source File: MapTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Maps blah file with a random offset and checks to see if data
 * written out to the file can be read back in
 */
private static void testWrite() throws Exception {
    StringBuilder sb = new StringBuilder();
    sb.setLength(4);

    for (int x=0; x<1000; x++) {
        try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {
            FileChannel fc = raf.getChannel();

            long offset = generator.nextInt(1000);
            MappedByteBuffer b = fc.map(MapMode.READ_WRITE,
                                        offset, 100);

            for (int i=0; i<4; i++) {
                b.put(i, (byte)('0' + i));
            }

            for (int i=0; i<4; i++) {
                byte aByte = b.get(i);
                sb.setCharAt(i, (char)aByte);
            }
            if (!sb.toString().equals("0123"))
                throw new Exception("Write test failed");
        }
    }
}
 
Example 14
Source File: Slopbucket.java    From jelectrum with MIT License 6 votes vote down vote up
public void addTrough(String name)
{
  if (name.length() > MAX_TROUGH_NAME_LEN) throw new RuntimeException("OVER MAX NAME LENGTH");

  Map<String, Integer> troughs = getTroughMap();

  if(troughs.containsKey(name)) return;

  if (!troughs.containsKey("__FREE"))
  throw new RuntimeException("TOO MANY TROUGHS");

  int trough_idx = troughs.get("__FREE");

  long hash_loc = makeNewHashTable(HASH_INITAL_SIZE);

  MappedByteBuffer mbb = getBufferMap(0);
  synchronized(mbb)
  {
    mbb.position( (int) (LOCATION_TROUGH_TABLE_START + (8 + MAX_TROUGH_NAME_LEN) * trough_idx));
    mbb.putLong(hash_loc);
    mbb.put(name.getBytes());
  }
  trough_map = null;
}
 
Example 15
Source File: LockingMappedFiles.java    From java-core-learning-example with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    fc = new RandomAccessFile("data.txt" , "rw").getChannel();
    MappedByteBuffer out = fc.map(FileChannel.MapMode.READ_WRITE,
            0 , LENGTH);
    for (int i = 0;i < LENGTH; i++)
        out.put((byte)'x');
    new LockAndModify(out,0,0 + LENGTH/3);
    new LockAndModify(out,LENGTH/2,LENGTH/2 + LENGTH/4);
}
 
Example 16
Source File: FileBasedMap.java    From syncer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void putBytes(MappedByteBuffer file, byte[] bytes) {
  for (int i = 0; i < bytes.length; i++) {
    file.put(i, bytes[i]);
  }
  int position = file.position();
  for (int i = bytes.length; i < position; i++) {
    file.put(i, (byte) 0);
  }
  file.position(bytes.length);
}
 
Example 17
Source File: NIODemo2.java    From cs-summary-reflection with Apache License 2.0 5 votes vote down vote up
@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 18
Source File: MissionDriver.java    From SoftwarePilot with MIT License 5 votes vote down vote up
void writePic(byte[] b) {
    try{
        File f = new File("../tmp/pictmp.jpg");
        f.delete();
        MappedByteBuffer out = new RandomAccessFile("../tmp/pictmp.jpg","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, b.length);

        for(int j = 0; j<b.length; j++){
            out.put(b[j]);
        }
    } catch(Exception e){
        e.printStackTrace();
    }
}
 
Example 19
Source File: LargeMappedFiles.java    From LearningOfThinkInJava with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    MappedByteBuffer out =
            new RandomAccessFile("./src/main/java/io/source/data.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE,0,length);
    for(int i=0;i<length;i++){
        out.put((byte)'x');
    }
    print("Finished Write");
    for(int i=length/2;i<length/2+6;i++){
        printnb((char)out.get(i));
    }
}
 
Example 20
Source File: StorageWriter.java    From PalDB with Apache License 2.0 4 votes vote down vote up
private File buildIndex(int keyLength)
    throws IOException {
  long count = keyCounts[keyLength];
  int slots = (int) Math.round(count / loadFactor);
  int offsetLength = maxOffsetLengths[keyLength];
  int slotSize = keyLength + offsetLength;

  // Init index
  File indexFile = new File(tempFolder, "index" + keyLength + ".dat");
  RandomAccessFile indexAccessFile = new RandomAccessFile(indexFile, "rw");
  try {
    indexAccessFile.setLength(slots * slotSize);
    FileChannel indexChannel = indexAccessFile.getChannel();
    MappedByteBuffer byteBuffer = indexChannel.map(FileChannel.MapMode.READ_WRITE, 0, indexAccessFile.length());

    // Init reading stream
    File tempIndexFile = indexFiles[keyLength];
    DataInputStream tempIndexStream = new DataInputStream(new BufferedInputStream(new FileInputStream(tempIndexFile)));
    try {
      byte[] keyBuffer = new byte[keyLength];
      byte[] slotBuffer = new byte[slotSize];
      byte[] offsetBuffer = new byte[offsetLength];

      // Read all keys
      for (int i = 0; i < count; i++) {
        // Read key
        tempIndexStream.readFully(keyBuffer);

        // Read offset
        long offset = LongPacker.unpackLong(tempIndexStream);

        // Hash
        long hash = (long) hashUtils.hash(keyBuffer);

        boolean collision = false;
        for (int probe = 0; probe < count; probe++) {
          int slot = (int) ((hash + probe) % slots);
          byteBuffer.position(slot * slotSize);
          byteBuffer.get(slotBuffer);

          long found = LongPacker.unpackLong(slotBuffer, keyLength);
          if (found == 0) {
            // The spot is empty use it
            byteBuffer.position(slot * slotSize);
            byteBuffer.put(keyBuffer);
            int pos = LongPacker.packLong(offsetBuffer, offset);
            byteBuffer.put(offsetBuffer, 0, pos);
            break;
          } else {
            collision = true;
            // Check for duplicates
            if (Arrays.equals(keyBuffer, Arrays.copyOf(slotBuffer, keyLength))) {
              throw new RuntimeException(
                      String.format("A duplicate key has been found for for key bytes %s", Arrays.toString(keyBuffer)));
            }
          }
        }

        if (collision) {
          collisions++;
        }
      }

      String msg = "  Max offset length: " + offsetLength + " bytes" +
              "\n  Slot size: " + slotSize + " bytes";

      LOGGER.log(Level.INFO, "Built index file {0}\n" + msg, indexFile.getName());
    } finally {
      // Close input
      tempIndexStream.close();

      // Close index and make sure resources are liberated
      indexChannel.close();
      indexChannel = null;
      byteBuffer = null;

      // Delete temp index file
      if (tempIndexFile.delete()) {
        LOGGER.log(Level.INFO, "Temporary index file {0} has been deleted", tempIndexFile.getName());
      }
    }
  } finally{
    indexAccessFile.close();
    indexAccessFile = null;
    System.gc();
  }

  return indexFile;
}