diff --git a/java/org/jlab/coda/hipo/Writer.java b/java/org/jlab/coda/hipo/Writer.java index 20160d30..006acc83 100644 --- a/java/org/jlab/coda/hipo/Writer.java +++ b/java/org/jlab/coda/hipo/Writer.java @@ -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 @@ -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, @@ -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"); @@ -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(); diff --git a/java/org/jlab/coda/hipo/WriterMT.java b/java/org/jlab/coda/hipo/WriterMT.java index f4f1bb3f..b9966409 100644 --- a/java/org/jlab/coda/hipo/WriterMT.java +++ b/java/org/jlab/coda/hipo/WriterMT.java @@ -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; @@ -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 @@ -561,6 +562,7 @@ 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, @@ -568,7 +570,20 @@ public final void open(String filename) throws HipoException { * 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"); } @@ -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 { @@ -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());