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

The following examples show how to use java.nio.MappedByteBuffer#force() . 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: IOUtils.java    From Android-utils with Apache License 2.0 6 votes vote down vote up
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 {
        safeCloseAll(fc);
    }
}
 
Example 2
Source File: Demo.java    From RocketMQ-Master-analyze with Apache License 2.0 6 votes vote down vote up
public static void readLine() throws Exception {
    File file = new File("E:\\demo");
    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
    MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 1024 * 10);
    int offset = 0;
    byte[] body = ("这里是上海" + System.lineSeparator()).getBytes();
    while (true) {
        if (offset + body.length <= 10240) {
            ByteBuffer slice = buffer.slice();
            slice.position(offset);
            slice.put(body);
            offset += body.length;
            buffer.force();
        }
        else {
            break;
        }
    }
    channel.close();
    System.out.println(file.getPath() + ".dat");
    file.renameTo(new File(file.getPath() + ".dat"));
}
 
Example 3
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 4
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 5
Source File: MappedBufferWrapper.java    From consulo with Apache License 2.0 6 votes vote down vote up
@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 6
Source File: Volume.java    From FastBootWeixin with Apache License 2.0 6 votes vote down vote up
@Override
public void sync() {
    if(readOnly) return;
    growLock.lock();
    try{
        for(ByteBuffer b: chunks){
            if(b!=null && (b instanceof MappedByteBuffer)){
                MappedByteBuffer bb = ((MappedByteBuffer) b);
                bb.force();
            }
        }

    }finally{
        growLock.unlock();
    }

}
 
Example 7
Source File: FileChannelTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
 */
public void test_map_Private() throws IOException {
    this.writeDataToFile(fileOfReadWriteFileChannel);
    MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.PRIVATE, 0,
            CONTENT_LENGTH);
    assertEquals(CONTENT_LENGTH, mapped.limit());
    // test copy on write if private
    ByteBuffer returnByPut = mapped.put(TEST_BYTES);
    assertSame(returnByPut, mapped);
    ByteBuffer checkBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
    mapped.force();
    readWriteFileChannel.read(checkBuffer);
    assertEquals(CONTENT, new String(checkBuffer.array(), "iso8859-1"));

    // test overflow
    try {
        mapped.put(("test" + CONTENT).getBytes("iso8859-1"));
        fail("should throw BufferOverflowException.");
    } catch (BufferOverflowException ex) {
        // expected;
    }
}
 
Example 8
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 9
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 10
Source File: FileIOUtils.java    From CrawlerForReader 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(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 11
Source File: DefaultMessageStoreTest.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
private void damageCommitlog(long offset) throws Exception {
    MessageStoreConfig messageStoreConfig = new MessageStoreConfig();
    File file = new File(messageStoreConfig.getStorePathCommitLog() + File.separator + "00000000000000000000");

    FileChannel fileChannel = new RandomAccessFile(file, "rw").getChannel();
    MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 1024 * 1024 * 10);

    int bodyLen = mappedByteBuffer.getInt((int) offset + 84);
    int topicLenIndex = (int) offset + 84 + bodyLen + 4;
    mappedByteBuffer.position(topicLenIndex);
    mappedByteBuffer.putInt(0);
    mappedByteBuffer.putInt(0);
    mappedByteBuffer.putInt(0);
    mappedByteBuffer.putInt(0);

    mappedByteBuffer.force();
    fileChannel.force(true);
    fileChannel.close();
}
 
Example 12
Source File: DefaultMessageStoreTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private void damageCommitlog(long offset) throws Exception {
    MessageStoreConfig messageStoreConfig = new MessageStoreConfig();
    File file = new File(messageStoreConfig.getStorePathCommitLog() + File.separator + "00000000000000000000");

    FileChannel fileChannel = new RandomAccessFile(file, "rw").getChannel();
    MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 1024 * 1024 * 10);

    int bodyLen = mappedByteBuffer.getInt((int) offset + 84);
    int topicLenIndex = (int) offset + 84 + bodyLen + 4;
    mappedByteBuffer.position(topicLenIndex);
    mappedByteBuffer.putInt(0);
    mappedByteBuffer.putInt(0);
    mappedByteBuffer.putInt(0);
    mappedByteBuffer.putInt(0);

    mappedByteBuffer.force();
    fileChannel.force(true);
    fileChannel.close();
}
 
Example 13
Source File: Slopbucket.java    From jelectrum with MIT License 5 votes vote down vote up
public synchronized void flush(boolean close)
  throws java.io.IOException
{
  for(MappedByteBuffer mbb : open_buffers.values())
  {
    mbb.force();
  }
  if (close)
  {
    open_buffers.clear();
    file_channel.close();
  }
     
}
 
Example 14
Source File: MemoryMappedFile.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void force(long offset, int length) throws IOException {
    _log.debug(String.format("forcing %s offset 0x%08x length 0x%08x", file.toString(), offset, length));
    MappedByteBuffer buff = null;
    try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
        FileChannel channel = raf.getChannel();
        buff = channel.map(MapMode.READ_WRITE, offset, size);
        buff.force();
    }
}
 
Example 15
Source File: MappedPageSourceIT.java    From offheap-store with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateLotsOfMappedBuffers() throws IOException {
  final int size = 1024 * 1024;
  final int count = 16;

  MappedPageSource source = new MappedPageSource(dataFile);
  try {
    byte[] data = new byte[1024];
    Arrays.fill(data, (byte) 0xff);

    List<MappedPage> pages = new ArrayList<>();
    for (int i = 0; i < count; i++) {
      pages.add(source.allocate(size, false, false, null));
    }

    for (MappedPage p : pages) {
      MappedByteBuffer b = p.asByteBuffer();
      while (b.hasRemaining()) {
        b.put(data);
      }
      b.force();
    }
  } finally {
    source.close();
  }

  try (RandomAccessFile raf = new RandomAccessFile(source.getFile(), "r")) {
    Assert.assertEquals(size * count, raf.length());
  }
}
 
Example 16
Source File: MappedPageImpl.java    From bigqueue with Apache License 2.0 5 votes vote down vote up
@Override
public void flush() {
	synchronized(this) {
		if (closed) return;
		if (dirty) {
			MappedByteBuffer srcBuf = (MappedByteBuffer)threadLocalBuffer.getSourceBuffer();
			srcBuf.force(); // flush the changes
			dirty = false;
			if (logger.isDebugEnabled()) {
				logger.debug("Mapped page for " + this.pageFile + " was just flushed.");
			}
		}
	}
}
 
Example 17
Source File: FileUtil.java    From Thunder with Apache License 2.0 5 votes vote down vote up
public static void forceDelete(File file) throws IOException {
    if (file.exists()) {
        SecureRandom random = new SecureRandom();
        @SuppressWarnings("resource")
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        FileChannel channel = raf.getChannel();
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, raf.length());
        // Overwrite with zeros
        while (buffer.hasRemaining()) {
            buffer.put((byte) 0);
        }
        buffer.force();
        buffer.rewind();
        // Overwrite with ones
        while (buffer.hasRemaining()) {
            buffer.put((byte) 0xFF);
        }
        buffer.force();
        buffer.rewind();
        // Overwrite with random data; one byte at a time
        byte[] data = new byte[1];
        while (buffer.hasRemaining()) {
            random.nextBytes(data);
            buffer.put(data[0]);
        }
        buffer.force();
        file.delete();
    }
}
 
Example 18
Source File: SequenceFileImpl.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
private void store(MappedByteBuffer buffer) {
  buffer.force();
}
 
Example 19
Source File: Space.java    From antsdb with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void force(long offsetStart, long size) throws IOException {
    MappedByteBuffer buf = this.mmf.channel.map(MapMode.READ_WRITE, offsetStart, size);
    buf.force();
    Unsafe.unmap(buf);
}
 
Example 20
Source File: RecordingCoordinator.java    From artio with Apache License 2.0 4 votes vote down vote up
private void saveRecordingIdsFile()
{
    libraryIdToExtendPosition.values().forEach(pos -> outboundRecordingIds.free.add(pos.recordingId));

    try
    {
        final int inboundSize = inboundRecordingIds.size();
        final int outboundSize = outboundRecordingIds.size();

        final File saveFile = File.createTempFile(FILE_NAME, "tmp", new File(configuration.logFileDir()));
        final int requiredLength = MessageHeaderEncoder.ENCODED_LENGTH + PreviousRecordingEncoder.BLOCK_LENGTH +
            InboundRecordingsEncoder.HEADER_SIZE + OutboundRecordingsEncoder.HEADER_SIZE +
            InboundRecordingsEncoder.recordingIdEncodingLength() * inboundSize +
            OutboundRecordingsEncoder.recordingIdEncodingLength() * outboundSize;
        final MappedByteBuffer mappedBuffer = IoUtil.mapExistingFile(saveFile, FILE_NAME, 0, requiredLength);
        final UnsafeBuffer buffer = new UnsafeBuffer(mappedBuffer);
        try
        {
            final MessageHeaderEncoder header = new MessageHeaderEncoder();
            final PreviousRecordingEncoder previousRecording = new PreviousRecordingEncoder();

            previousRecording.wrapAndApplyHeader(buffer, 0, header);

            final InboundRecordingsEncoder inbound = previousRecording.inboundRecordingsCount(inboundSize);
            inboundRecordingIds.forEach(id -> inbound.next().recordingId(id));

            final OutboundRecordingsEncoder outbound = previousRecording.outboundRecordingsCount(outboundSize);
            outboundRecordingIds.forEach(id -> outbound.next().recordingId(id));

            mappedBuffer.force();
        }
        finally
        {
            IoUtil.unmap(mappedBuffer);
        }

        Files.move(saveFile.toPath(), recordingIdsFile.toPath(), ATOMIC_MOVE, REPLACE_EXISTING);
    }
    catch (final Throwable e)
    {
        e.printStackTrace();
        errorHandler.onError(e);
    }
}