amidstest/src/amidst/map/ByteArrayHub.java
Skiphs dafa8abd79 First implementation of new map
There are still a lot of issues, such as the lack of all the old layers,
left over code, and a few missing features (move to for example) But
this is a start.

Note that caching methods should not be bothered with for the moment, as
a cache directory and expiration date have not been coded.
2013-04-11 06:33:23 -07:00

96 lines
2.6 KiB
Java

package amidst.map;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import amidst.Log;
public class ByteArrayHub {
private File path;
private long key;
private byte unitSize;
private int maxUnits;
private int dataHop;
public int activeFragments = 0;
private byte[] data;
private byte[] returnCache;
private int unitOffset;
public ByteArrayHub(long key, byte unitSize, int maxUnits, String strPath) {
this.unitSize = unitSize;
this.maxUnits = maxUnits;
this.key = key;
unitOffset = maxUnits*unitSize;
path = new File(strPath + Long.toHexString(key).toUpperCase() + ".acache");
data = new byte[unitOffset*ByteArrayCache.CACHE_MAX_SIZE + ByteArrayCache.HEADER_SIZE];
returnCache = new byte[unitSize*maxUnits];
if (path.exists()) {
GZIPInputStream inStream;
try {
inStream = new GZIPInputStream(new FileInputStream(path));
byte[] readBuffer = new byte[1024];
int len;
int offset = 0;
while ((len = inStream.read(readBuffer)) != -1) {
System.arraycopy(readBuffer, 0, data, offset, len);
offset += len;
}
//Log.i("@@@@@ " + inStream.read(data));
inStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public boolean exists(int id) {
return (((data[id >> 3] >> (id % 8)) & 0x1) == 0x1);
}
public byte[] get(int id) {
System.arraycopy(data, ByteArrayCache.HEADER_SIZE + unitOffset*id, returnCache, 0, unitOffset);
return returnCache;
}
public void put(int id, byte[] indata) {
System.arraycopy(indata, 0, data, ByteArrayCache.HEADER_SIZE + unitOffset*id, unitOffset);
data[id >> 3] |= 0x1 << (id % 8);
}
public void unload() {
try {
BufferedOutputStream outStream = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(path)));
outStream.write(data, 0, data.length);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public long getKey() {
return key;
}
}