Skip to content

Commit

Permalink
handle new boolean arg whether or not to allow file overwriting
Browse files Browse the repository at this point in the history
  • Loading branch information
carltimmer committed Dec 9, 2024
1 parent 22f4229 commit c36eb71
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
40 changes: 35 additions & 5 deletions java/org/jlab/coda/hipo/Writer.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ public void addTrailerWithIndex(boolean addTrailingIndex) {

/**
* Open a new file and write file header with no user header.
* Will not overwrite existing file.
* Existing file is NOT overwritten.
* @param filename output file name
* @throws HipoException if open already called without being followed by calling close.
* @throws IOException if file cannot be found or IO error writing to file
Expand All @@ -435,6 +437,7 @@ public final void open(String filename) throws HipoException, IOException {
/**
* Open a file and write file header with given user header.
* User header is automatically padded when written.
* Existing file is NOT overwritten.
* @param filename name of file to write to.
* @param userHeader byte array representing the optional user's header.
* If this is null AND dictionary and/or first event are given,
Expand All @@ -446,6 +449,27 @@ public final void open(String filename) throws HipoException, IOException {
*/
public final void open(String filename, byte[] userHeader)
throws HipoException, IOException {
open (filename, userHeader, false);
}


/**
* Open a file and write file header with given user header.
* User header is automatically padded when written.
* @param filename name of file to write to.
* @param userHeader byte array representing the optional user's header.
* If this is null AND dictionary and/or first event are given,
* the dictionary and/or first event will be placed in its
* own record and written as the user header.
* @param overwrite if true, overwrite any existing file.
* @throws HipoException filename arg is null, if constructor specified writing to a buffer,
* or if open() was already called without being followed by reset().
* @throws IOException if IO error writing to file,
* or if overwrite is false and file exists.
*/
public final void open(String filename, byte[] userHeader, boolean overwrite)
throws HipoException, IOException {


if (opened) {
throw new HipoException("currently open, call reset() first");
Expand Down Expand Up @@ -482,11 +506,17 @@ else if (!toFile) {
// Path object corresponding to file currently being written
Path currentFilePath = Paths.get(filename);

asyncFileChannel = AsynchronousFileChannel.open(currentFilePath,
//StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.CREATE_NEW,
StandardOpenOption.CREATE,
StandardOpenOption.WRITE);
if (overwrite) {
asyncFileChannel = AsynchronousFileChannel.open(currentFilePath,
StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.CREATE,
StandardOpenOption.WRITE);
}
else {
asyncFileChannel = AsynchronousFileChannel.open(currentFilePath,
StandardOpenOption.CREATE_NEW,
StandardOpenOption.WRITE);
}

asyncFileChannel.write(headBuffer, 0L);
fileWritingPosition = fileHeader.getLength();
Expand Down
45 changes: 36 additions & 9 deletions java/org/jlab/coda/hipo/WriterMT.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.jlab.coda.jevio.EvioNode;
import org.jlab.coda.jevio.Utilities;

import java.io.FileNotFoundException;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -549,6 +549,7 @@ public void addTrailerWithIndex(boolean addTrailingIndex) {

/**
* Open a new file and write file header with no user header.
* Existing file is NOT overwritten.
* @param filename output file name
* @throws HipoException if filename arg is null or bad,
* if this method already called without being
Expand All @@ -561,14 +562,28 @@ public final void open(String filename) throws HipoException {
/**
* Open a file and write file header with given user's header.
* User header is automatically padded when written.
* Existing file is NOT overwritten.
* @param filename disk file name.
* @param userHdr byte array representing the optional user's header.
* @throws HipoException if filename arg is null or bad,
* if this method already called without being
* followed by reset.
*/
public final void open(String filename, byte[] userHdr) throws HipoException {
open(filename, userHdr, false);
}

/**
* Open a file and write file header with given user's header.
* User header is automatically padded when written.
* @param filename disk file name.
* @param userHdr byte array representing the optional user's header.
* @param overwrite if true, overwrite any existing file.
* @throws HipoException if filename arg is null or bad,
* if this method already called without being followed by reset,
* or if overwrite is false and file exists.
*/
public final void open(String filename, byte[] userHdr, boolean overwrite) throws HipoException {
if (opened) {
throw new HipoException("currently open, call reset() first");
}
Expand All @@ -583,7 +598,7 @@ public final void open(String filename, byte[] userHdr) throws HipoException {
// User header given as arg has precedent
if (userHdr != null) {
haveUserHeader = true;
System.out.println("writerMT::open: given a valid user header to write");
//System.out.println("writerMT::open: given a valid user header to write");
fileHeaderBuffer = createHeader(userHdr);
}
else {
Expand All @@ -594,20 +609,32 @@ public final void open(String filename, byte[] userHdr) throws HipoException {
// else place dictionary and/or firstEvent into
// record which becomes user header
else {
System.out.println("writerMT::open: given a valid dict/first ev header to write");
//System.out.println("writerMT::open: given a valid dict/first ev header to write");
fileHeaderBuffer = createHeader(dictionaryFirstEventBuffer);
}
}

try {
outStream = new RandomAccessFile(filename, "rw");
if (overwrite) {
outStream = new RandomAccessFile(filename, "rw");
// Truncate the file to 0 length to overwrite it
outStream.setLength(0);
}
else {
File file = new File(filename);

// Check if the file exists
if (file.exists()) {
throw new HipoException("File already exists: " + file.getName());
}

outStream = new RandomAccessFile(filename, "rw");
}
fileChannel = outStream.getChannel();
outStream.write(fileHeaderBuffer.array());

} catch (FileNotFoundException ex) {
Logger.getLogger(WriterMT.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(WriterMT.class.getName()).log(Level.SEVERE, null, ex);
}
catch (Exception ex) {
throw new HipoException(ex);
}

writerBytesWritten = (long) (fileHeader.getLength());
Expand Down

0 comments on commit c36eb71

Please sign in to comment.