Skip to content

Instantly share code, notes, and snippets.

@scottcagno
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save scottcagno/27b2a8abd1ac48c0b72f to your computer and use it in GitHub Desktop.

Select an option

Save scottcagno/27b2a8abd1ac48c0b72f to your computer and use it in GitHub Desktop.
Block Style Memory Mapped File Utility Class
import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;
/**
* Created by Scott Cagno.
* Copyright Cagno Solutions. All rights reserved.
*/
public final class MMapFile {
private static int BLOCK_SIZE = 1024*2;
private static int BLOCKS_PER_FILE = 2048;
private static int FILE_SIZE = BLOCKS_PER_FILE *BLOCK_SIZE;
private static int BLOCK = 0;
private static byte[] NEW_BLOCK = new byte[BLOCK_SIZE];
private File fd;
private FileChannel ch;
private MappedByteBuffer buf;
public MMapFile(String path) {
this.fd = new File(path);
}
public void setBlockSize(int blockSize) {
BLOCK_SIZE = blockSize;
FILE_SIZE = BLOCKS_PER_FILE *BLOCK_SIZE;
NEW_BLOCK = new byte[BLOCK_SIZE];
}
public void setBlocksPerFile(int blocksPerPage) {
BLOCKS_PER_FILE = blocksPerPage;
FILE_SIZE = BLOCKS_PER_FILE *BLOCK_SIZE;
}
public void open() {
try {
this.ch = new RandomAccessFile(this.fd, "rw").getChannel();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
this.buf = ch.map(FileChannel.MapMode.READ_WRITE, 0, FILE_SIZE);
} catch (IOException e) {
e.printStackTrace();
}
}
public int alignBlock(int n) {
BLOCK = n;
if(BLOCK >= BLOCKS_PER_FILE)
return -1;
return buf.position(BLOCK*BLOCK_SIZE).position();
}
public byte[] read() {
byte[] dst = NEW_BLOCK;
buf.position(alignBlock(BLOCK));
buf.get(dst, 0, BLOCK_SIZE);
BLOCK++;
return dst;
}
public byte[] read(int block) {
byte[] dst = NEW_BLOCK;
buf.position(alignBlock(block));
buf.get(dst, 0, BLOCK_SIZE);
return dst;
}
public void write(byte[] data) {
if(hasRemaining()) {
buf.position(alignBlock(BLOCK));
buf.put(Arrays.copyOf(data, BLOCK_SIZE));
BLOCK++;
}
}
public void write(byte[] data, int block) {
if(hasRemaining()) {
buf.position(alignBlock(block));
buf.put(Arrays.copyOf(data, BLOCK_SIZE));
}
}
public boolean hasRemaining() {
return buf.hasRemaining() && buf.remaining() >= BLOCK_SIZE && BLOCK < BLOCKS_PER_FILE;
}
public boolean deleteFile() {
return this.fd.delete();
}
public void close() {
try {
this.ch.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static byte[] serialize(Object obj) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(obj);
return out.toByteArray();
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
public static Object deserialize(byte[] data) throws IOException, ClassNotFoundException {
try {
ByteArrayInputStream in = new ByteArrayInputStream(data);
ObjectInputStream is = new ObjectInputStream(in);
return is.readObject();
} catch (IOException | ClassNotFoundException ex) {
ex.printStackTrace();
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment