Skip to content

Commit

Permalink
add test AsynchronousFileChannel's effect on reading buf
Browse files Browse the repository at this point in the history
  • Loading branch information
carltimmer committed Jul 16, 2024
1 parent 77108a0 commit f14988e
Showing 1 changed file with 56 additions and 7 deletions.
63 changes: 56 additions & 7 deletions java/org/jlab/coda/jevio/test/FileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Future;


/**
* Test program.
Expand All @@ -17,8 +23,53 @@
*/
public class FileTest {



/**
* Test how the AsynchronousFileChannel affect its ByteBuffer arg
* when reading and writing. Does buffer position advance on write?
* on read?
*
* @param args
*/
public static void main(String args[]) throws IOException {
String fileName = "/tmp/fileTest";

Path currentFilePath = Paths.get(fileName);
File currentFile = currentFilePath.toFile();

ByteBuffer buffer = ByteBuffer.allocate(64);

buffer.putInt(1);
buffer.flip();

System.out.println("Before write pos = " + buffer.position() + ", remaining = " + buffer.remaining());

// For reading existing file and preparing for stream writes
AsynchronousFileChannel asyncFileChannel = AsynchronousFileChannel.open(currentFilePath,
StandardOpenOption.READ,
StandardOpenOption.WRITE);


Future<Integer> future = asyncFileChannel.write(buffer, 0);

try {
int partial = future.get();
}
catch (Exception e) {
throw new IOException(e);
}

asyncFileChannel.write(buffer, 0);
asyncFileChannel.close();

System.out.println("After write pos = " + buffer.position());
}



/** For WRITING a local file. */
public static void main1(String args[]) {
public static void main4(String args[]) {
File f = new File("/dev/shm/someTestFile");
long freeSpace = f.getFreeSpace();
File f2 = new File("/dev/shm");
Expand All @@ -35,7 +86,7 @@ public static void main1(String args[]) {
/**
* For WRITING an event too large for the desired record size.
*/
public static void main(String args[]) {
public static void main5(String args[]) {

// String fileName = "./myData.ev";
String fileName = "./codaFileTest.ev";
Expand Down Expand Up @@ -90,12 +141,10 @@ public static void main(String args[]) {
}




/**
* For WRITING an event too large for the desired record size.
*/
public static void main0(String args[]) {
public static void main6(String args[]) {

// String fileName = "./myData.ev";
String fileName = "./codaFileTest.ev";
Expand Down Expand Up @@ -213,7 +262,7 @@ public static void main0(String args[]) {
* For WRITING a single event repeatedly to a local file in order to
* test the file structure to see if record sizes are correct.
*/
public static void main11(String args[]) {
public static void main7(String args[]) {

// String fileName = "./myData.ev";
String fileName = "./codaFileTest.ev";
Expand Down Expand Up @@ -266,7 +315,7 @@ public static void main11(String args[]) {


/** For WRITING a local file. */
public static void main2(String args[]) {
public static void main8(String args[]) {

// String fileName = "./myData.ev";
String fileName = "/home/timmer/fileTestSmall.ev";
Expand Down

0 comments on commit f14988e

Please sign in to comment.