1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
package mpjdev.buffertest;
import mpjdev.*;
import mpjbuf.*;
import java.util.Arrays;
import java.nio.*;
import java.nio.channels.FileChannel.*;
import java.nio.channels.*;
import java.io.*;
public class MappedBuffer {
public MappedBuffer() {
}
static public void main(String[] args) throws Exception {
try {
MappedBuffer a = new MappedBuffer(args);
}
catch (Exception e) {
}
}
public MappedBuffer(String[] args) throws Exception {
int DATA_SIZE = 100;
File f = new File("mappedfile1");
RandomAccessFile raf = new RandomAccessFile(f, "rw");
FileChannel fileChannel = raf.getChannel();
MappedByteBuffer mappedByteBuffer = fileChannel.map(MapMode.READ_WRITE, 0,
400);// f.length());
MPJDev.init(args);
int intArray[] = new int[DATA_SIZE];
for (int k = 1; k < 2; k++) {
for (int i = 0; i < DATA_SIZE; i++) {
intArray[i] = 8;
}
if (MPJDev.WORLD.id() == 0) {
for (int i = 0; i < DATA_SIZE; i++) {
mappedByteBuffer.putInt(intArray[i]);
}
// mappedByteBuffer.flip();
} else if (MPJDev.WORLD.id() == 1) {
/* Just make sure that this reads when the data is in there */
try {
Thread.currentThread().sleep(1000);
}
catch (Exception e) {
}
int intReadArray[] = new int[DATA_SIZE];
for (int i = 0; i < DATA_SIZE; i++) {
intReadArray[i] = 9;
}
for (int i = 0; i < DATA_SIZE; i++) {
intReadArray[i] = mappedByteBuffer.getInt();
}
if (Arrays.equals(intArray, intReadArray)) {
System.out.println("\n#################" + "\n <<<<PASSED>>>> "
+ "\n################");
} else {
System.out.println("\n#################" + "\n <<<<FAILED>>>> "
+ "\n################");
}
}
}
try {
Thread.currentThread().sleep(10000);
}
catch (Exception e) {
e.printStackTrace();
}
raf.close();
// f.close();
fileChannel.close();
MPJDev.finish();
}
}
|