diff --git a/java/org/jlab/coda/jevio/EvioReaderUnsyncV6.java b/java/org/jlab/coda/jevio/EvioReaderUnsyncV6.java index b1552a01..b676b7dd 100644 --- a/java/org/jlab/coda/jevio/EvioReaderUnsyncV6.java +++ b/java/org/jlab/coda/jevio/EvioReaderUnsyncV6.java @@ -44,16 +44,16 @@ public class EvioReaderUnsyncV6 implements IEvioReader { /** The reader object which does all the work. */ - private Reader reader; + protected Reader reader; /** Is this object currently closed? */ - private boolean closed; + protected boolean closed; /** Root element tag for XML file */ - private static final String ROOT_ELEMENT = "evio-data"; + protected static final String ROOT_ELEMENT = "evio-data"; /** Parser object for file/buffer. */ - private EventParser parser; + protected EventParser parser; //------------------------ diff --git a/java/org/jlab/coda/jevio/EvioReaderV6.java b/java/org/jlab/coda/jevio/EvioReaderV6.java index 8b55f089..cf738098 100644 --- a/java/org/jlab/coda/jevio/EvioReaderV6.java +++ b/java/org/jlab/coda/jevio/EvioReaderV6.java @@ -10,16 +10,9 @@ package org.jlab.coda.jevio; -import org.jlab.coda.hipo.*; -import org.jlab.coda.hipo.Reader; - -import javax.xml.stream.FactoryConfigurationError; -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamWriter; -import java.io.*; +import java.io.File; +import java.io.IOException; import java.nio.ByteBuffer; -import java.nio.ByteOrder; /** * This class is used to read an evio version 6 format file or buffer. @@ -35,25 +28,13 @@ * for access to the structures. For those familiar with XML, the event is processed DOM-like. *

* + * This class is a thread safe version of EvioReaderUnsyncV6. + * * @since version 6 * @author timmer */ -public class EvioReaderV6 implements IEvioReader { - - /** The reader object which does all the work. */ - private Reader reader; - - /** Is this object currently closed? */ - private boolean closed; - - /** Root element tag for XML file */ - private static final String ROOT_ELEMENT = "evio-data"; +public class EvioReaderV6 extends EvioReaderUnsyncV6 { - /** Parser object for file/buffer. */ - private EventParser parser; - - - //------------------------ /** * Constructor for reading an event file. @@ -65,7 +46,7 @@ public class EvioReaderV6 implements IEvioReader { * @throws EvioException if file arg is null */ public EvioReaderV6(String path) throws EvioException, IOException { - this(new File(path)); + super(path); } /** @@ -82,7 +63,7 @@ public EvioReaderV6(String path) throws EvioException, IOException { * if first record number != 1 when checkRecNumSeq arg is true */ public EvioReaderV6(String path, boolean checkRecNumSeq) throws EvioException, IOException { - this(new File(path), checkRecNumSeq); + super(path, checkRecNumSeq); } /** @@ -95,7 +76,7 @@ public EvioReaderV6(String path, boolean checkRecNumSeq) throws EvioException, I * @throws EvioException if file arg is null */ public EvioReaderV6(File file) throws EvioException, IOException { - this(file, false); + super(file); } /** @@ -115,14 +96,7 @@ public EvioReaderV6(File file) throws EvioException, IOException { */ EvioReaderV6(File file, boolean checkRecNumSeq) throws EvioException, IOException { - - try { - reader = new Reader(file.getPath(), checkRecNumSeq); - parser = new EventParser(); - } - catch (HipoException e) { - throw new EvioException(e); - } + super(file, checkRecNumSeq); } @@ -134,7 +108,7 @@ public EvioReaderV6(File file) throws EvioException, IOException { * @throws EvioException if buffer arg is null */ public EvioReaderV6(ByteBuffer byteBuffer) throws EvioException { - this(byteBuffer, false); + super(byteBuffer); } /** @@ -149,19 +123,17 @@ public EvioReaderV6(ByteBuffer byteBuffer) throws EvioException { * buf not in evio format. */ public EvioReaderV6(ByteBuffer byteBuffer, boolean checkRecNumSeq) throws EvioException { - - try { - reader = new Reader(byteBuffer); - if (!reader.isEvioFormat()) { - throw new EvioException("buffer not in evio format"); - } - parser = new EventParser(); - } - catch (HipoException e) { - throw new EvioException(e); - } + super(byteBuffer, checkRecNumSeq); } + + //-------------------------------------------------------------- + // The following methods overwrite the equivalent methods in + // EventReaderUnsyncV6. They are mutex protected for thread + // safety. + //-------------------------------------------------------------- + + /** * This method can be used to avoid creating additional EvioReader * objects by reusing this one with another buffer. The method @@ -175,274 +147,54 @@ public EvioReaderV6(ByteBuffer byteBuffer, boolean checkRecNumSeq) throws EvioEx */ @Override public synchronized void setBuffer(ByteBuffer buf) throws EvioException, IOException { - try { - reader.setBuffer(buf); - if (!reader.isEvioFormat()) { - throw new EvioException("buffer not in evio format"); - } - } - catch (HipoException e) {/* never thrown cause buf never null */} + super.setBuffer(buf); } /** {@inheritDoc} */ @Override - public synchronized boolean isClosed() {return reader.isClosed();} - - /** {@inheritDoc} */ - @Override - public boolean checkBlockNumberSequence() {return reader.getCheckRecordNumberSequence();} - - /** {@inheritDoc} */ - @Override - public ByteOrder getByteOrder() {return reader.getByteOrder();} - - /** {@inheritDoc} */ - @Override - public int getEvioVersion() {return reader.getVersion();} - - /** {@inheritDoc} */ - @Override - public String getPath() {return reader.getFileName();} - - /** - * Get the file/buffer parser. - * @return file/buffer parser. - */ - @Override - public EventParser getParser() { - return parser; - } - - /** - * Set the file/buffer parser. - * @param parser file/buffer parser. - */ - @Override - public void setParser(EventParser parser) { - if (parser != null) { - this.parser = parser; - } - } + public synchronized boolean isClosed() {return super.isClosed();} /** {@inheritDoc} */ @Override - public String getDictionaryXML() {return reader.getDictionary();} - - /** {@inheritDoc} */ - @Override - public boolean hasDictionaryXML() {return reader.hasDictionary();} - - /** {@inheritDoc} */ - @Override - public EvioEvent getFirstEvent() { - try { - byte[] rawBytes = reader.getFirstEvent(); - return EvioReader.getEvent(rawBytes, 0, reader.getByteOrder()); - } - catch (EvioException e) {} - - return null; - } - - /** {@inheritDoc} */ - @Override - public boolean hasFirstEvent() {return reader.hasFirstEvent();} - - /** - * Get the number of events remaining in the file. - * Useful only if doing a sequential read. - * - * @return number of events remaining in the file - */ - @Override - public int getNumEventsRemaining() throws EvioException {return reader.getNumEventsRemaining();} - - /** {@inheritDoc} */ - @Override - public ByteBuffer getByteBuffer() {return reader.getBuffer();} - - /** {@inheritDoc} */ - @Override - public long fileSize() {return reader.getFileSize();} - - - /** {@inheritDoc} */ - @Override - public IBlockHeader getFirstBlockHeader() {return reader.getFirstRecordHeader();} - - - /** - * Get the event in the file/buffer at a given index (starting at 1). - * As useful as this sounds, most applications will probably call - * {@link #parseNextEvent()} or {@link #parseEvent(int)} instead, - * since it combines combines getting an event with parsing it.

- * - * @param index the event number in a 1,2,..N counting sense, from beginning of file/buffer. - * @return the event in the file/buffer at the given index or null if none - * @throws EvioException if failed read due to bad file/buffer format; - * if out of memory; - * if index out of bounds; - * if object closed - */ - @Override public synchronized EvioEvent getEvent(int index) throws EvioException { return EvioReader.getEvent(getEventArray(index), 0, reader.getByteOrder()); } - /** {@inheritDoc} */ @Override public synchronized EvioEvent parseEvent(int index) throws EvioException { - EvioEvent event = getEvent(index); - if (event != null) parseEvent(event); - return event; + return super.parseEvent(index); } - /** {@inheritDoc} */ @Override public synchronized EvioEvent nextEvent() throws EvioException { - - if (closed) { - throw new EvioException("object closed"); - } - - byte[] bytes; - try { - bytes = reader.getNextEvent(); - } - catch (HipoException e) { - throw new EvioException(e); - } - - return EvioReader.getEvent(bytes, 0, reader.getByteOrder()); + return super.nextEvent(); } - /** {@inheritDoc} */ @Override public synchronized EvioEvent parseNextEvent() throws IOException, EvioException { - EvioEvent event = nextEvent(); - if (event != null) { - parseEvent(event); - } - return event; + return super.parseNextEvent(); } - /** {@inheritDoc} */ - @Override - public void parseEvent(EvioEvent evioEvent) throws EvioException { - // This method is synchronized too - parser.parseEvent(evioEvent); - } - - - /** {@inheritDoc} */ - @Override - public byte[] getEventArray(int eventNumber) throws EvioException { - - if (closed) { - throw new EvioException("object closed"); - } - - try { - byte[] evBytes = reader.getEvent(eventNumber - 1); - if (evBytes == null) { - throw new EvioException("eventNumber (" + eventNumber + ") is out of bounds"); - } - return evBytes; - } - catch (HipoException e) { - throw new EvioException(e); - } - } - - - /** {@inheritDoc} */ - @Override - public ByteBuffer getEventBuffer(int eventNumber) - throws EvioException, IOException { - return ByteBuffer.wrap(getEventArray(eventNumber)).order(reader.getByteOrder()); - } - - - /** This method is not relevant in evio 6 and does nothing. */ - @Override - public void rewind() {} - - - /** This method is not relevant in evio 6, does nothing, and returns 0. */ @Override - public synchronized long position() {return 0L;} + public synchronized long position() {return super.position();} - - /** - * This is closes the file, but for buffers it only sets the position to 0. - * @throws IOException if error accessing file - */ + /** {@inheritDoc} */ @Override public synchronized void close() throws IOException { - if (closed) { - return; - } - - reader.close(); - closed = true; + super.close(); } - /** {@inheritDoc} */ - @Override - public IBlockHeader getCurrentBlockHeader() {return reader.getCurrentRecordStream().getHeader();} - - - /** - * In this version, this method is a wrapper on {@link #parseEvent(int)}. - * - * @deprecated use {@link #parseEvent(int)}. - * @param evNumber the event number from the start of the file starting at 1. - * @return the specified event in file or null if there's an error or nothing at that event #. - * @throws EvioException if object closed - */ @Override @Deprecated public synchronized EvioEvent gotoEventNumber(int evNumber) throws EvioException { - - if (closed) { - throw new EvioException("object closed"); - } - - try { - return parseEvent(evNumber); - } - catch (EvioException e) { - return null; - } + return super.gotoEventNumber(evNumber); } - - /** {@inheritDoc} */ - @Override - public WriteStatus toXMLFile(String path) throws EvioException { - return toXMLFile(path, false); - } - - - /** {@inheritDoc} */ - @Override - public WriteStatus toXMLFile(String path, boolean hex) throws EvioException { - return toXMLFile(path, null, hex); - } - - /** {@inheritDoc} */ - @Override - public WriteStatus toXMLFile(String path, IEvioProgressListener progressListener) - throws EvioException { - return toXMLFile(path, progressListener, false); - } - - /** {@inheritDoc} */ @Override public synchronized WriteStatus toXMLFile(String path, @@ -450,101 +202,19 @@ public synchronized WriteStatus toXMLFile(String path, boolean hex) throws EvioException { - if (closed) { - throw new EvioException("object closed"); - } - - FileOutputStream fos; - - try { - fos = new FileOutputStream(path); - } - catch (FileNotFoundException e) { - e.printStackTrace(); - return WriteStatus.CANNOT_OPEN_FILE; - } - - try { - XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(fos); - xmlWriter.writeStartDocument(); - xmlWriter.writeCharacters("\n"); - xmlWriter.writeComment("Event source file: " + path); - - // start the root element - xmlWriter.writeCharacters("\n"); - xmlWriter.writeStartElement(ROOT_ELEMENT); - xmlWriter.writeAttribute("numevents", "" + getEventCount()); - xmlWriter.writeCharacters("\n"); - - int eventCount = getEventCount(); - - // Loop through the events - EvioEvent event; - try { - for (int i=1; i <= eventCount; i++) { - event = parseEvent(i); - event.toXML(xmlWriter, hex); - // Anybody interested in progress? - if (progressListener != null) { - progressListener.completed(event.getEventNumber(), eventCount); - } - } - } - catch (EvioException e) { - e.printStackTrace(); - return WriteStatus.UNKNOWN_ERROR; - } - - // done. Close root element, end the document, and flush. - xmlWriter.writeEndElement(); - xmlWriter.writeEndDocument(); - xmlWriter.flush(); - xmlWriter.close(); - - try { - fos.close(); - } - catch (IOException e) { - e.printStackTrace(); - } - } - catch (XMLStreamException e) { - e.printStackTrace(); - return WriteStatus.UNKNOWN_ERROR; - } - catch (FactoryConfigurationError e) { - return WriteStatus.UNKNOWN_ERROR; - } - catch (EvioException e) { - return WriteStatus.EVIO_EXCEPTION; - } - - return WriteStatus.SUCCESS; + return super.toXMLFile(path, progressListener, hex); } - /** {@inheritDoc} */ @Override public synchronized int getEventCount() throws EvioException { - - if (closed) { - throw new EvioException("object closed"); - } - - return reader.getEventCount(); + return super.getEventCount(); } - /** {@inheritDoc} */ @Override public synchronized int getBlockCount() throws EvioException { - - if (closed) { - throw new EvioException("object closed"); - } - - return reader.getRecordCount(); + return super.getBlockCount(); } - } \ No newline at end of file