Code Monkey home page Code Monkey logo

mmf4j's Introduction

MemoryMappedFiles4Java Build Status

Description

This library aims to bring memory mapped files to java. The goal is to give better control over the creation, modification and destruction in contrast to already present MappedByteBuffer. It tries to unify the interface for using such maps on different operating systems which means that a lot of details are not possible to implement. Furthermore, there might be some cases where you might need to do tedious work that may not be required on your target platform.

This library supports windows and linux.

Example

For each file you need to create a new MemoryMap object and give it a path to a file:

MemoryMap mm = MemoryMapFactory.getInstance();
mm.openFile("./test.txt");

At least on windows you need to open a mapping with the estimated filesize:

mm.openMapping(100);

Now we are done with our preparation and we can start with mapping parts of the file (called a view) to the memory. The memory is presented as a simple buffer where you can read and write from. The first argument is the offset from the beginning of the file and the second one is the size you want to be able to access.

ByteBuf b = mm.mapView(20, 10);

In this example we write the string "Hello" at position 20 in the file.

b.writeBytes("Hello".getBytes(CharsetUtil.UTF_8));

Of course you can overwrite already written content easily:

b.writerIndex(0);
b.writeBytes("World".getBytes(CharsetUtil.UTF_8));

Or if you prefer the direct way:

b.setBytes(0, "Java".getBytes(CharsetUtil.UTF_8));

We can also read from the map:

byte[] bytes = new byte[5];
b.getBytes(0, bytes);
System.out.println(new String(bytes, CharsetUtil.UTF_8));

When the work of this buffer is done we need to release it:

b.release();

When we are done with all file operations we need to truncate the file to the expected size and close it:

mm.truncateFile(25);
mm.close();

Legal

This library proudly uses the buffers of the Netty library.
This library proudly uses JNA for making native calls.
This library is released under the Apache License Version 2.0.

mmf4j's People

Contributors

nithanim avatar

Stargazers

Julian avatar Ashwin Jayaprakash avatar Davide Savazzi avatar Nico Hezel avatar Peter avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.