Added comments and am in the process of testing

This commit is contained in:
U-BASIS\dsmyda 2019-11-07 17:01:18 -05:00
parent ee3c0c01d2
commit 859f1faba9

View File

@ -27,17 +27,16 @@ import java.nio.file.Path;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
/** /**
* Extracts XRY entities and determines the report type. An * Extracts XRY entities and determines the report type. An example of an XRY
* example of an XRY entity would be: * entity would be:
* *
* Calls # 1 * Calls # 1
* Call Type: Missed * Call Type: Missed
* Time: 1/2/2019 1:23:45 PM (Device) * Time: 1/2/2019 1:23:45 PM (Device)
* From * From
* Tel: 12345678 * Tel: 12345678
*
*/ */
public final class XRYFileReader { public final class XRYFileReader implements AutoCloseable {
///Assume UTF_16LE ///Assume UTF_16LE
private static final Charset CHARSET = StandardCharsets.UTF_16LE; private static final Charset CHARSET = StandardCharsets.UTF_16LE;
@ -45,25 +44,26 @@ public final class XRYFileReader {
//Assume all headers are 5 lines in length. //Assume all headers are 5 lines in length.
private static final int HEADER_LENGTH_IN_LINES = 5; private static final int HEADER_LENGTH_IN_LINES = 5;
//Underlying reader for the xry file.
private final BufferedReader reader; private final BufferedReader reader;
private final StringBuilder xryEntity; private final StringBuilder xryEntity;
/** /**
* Creates an XRYFileReader. As part of construction, the file handles are * Creates an XRYFileReader. As part of construction, the file handles are
* opened and reader is advanced passed all header lines of the XRY file. * opened and reader is advanced passed all header lines of the XRY file.
* *
* The file is assumed to be encoded in UTF-16LE. * The file is assumed to be encoded in UTF-16LE.
* *
* @param xryFile XRY file to read. It is assumed that the caller has read * @param xryFile XRY file to read. It is assumed that the caller has read
* access to the path. * access to the path.
* @throws IOException if an I/O error occurs. * @throws IOException if an I/O error occurs.
*/ */
public XRYFileReader(Path xryFile) throws IOException { public XRYFileReader(Path xryFile) throws IOException {
reader = Files.newBufferedReader(xryFile, CHARSET); reader = Files.newBufferedReader(xryFile, CHARSET);
//Advance reader to start of the first XRY entity. //Advance the reader to the start of the first XRY entity.
for(int i = 0; i < HEADER_LENGTH_IN_LINES; i++) { for (int i = 0; i < HEADER_LENGTH_IN_LINES; i++) {
reader.readLine(); reader.readLine();
} }
@ -71,16 +71,18 @@ public final class XRYFileReader {
} }
/** /**
* Advances the reader until the next valid XRY entity is detected.
* *
* @return * @return Indication that there is another XRY entity to consume or that
* @throws IOException * the file has been exhausted.
* @throws IOException if an I/O error occurs.
*/ */
public boolean hasNextEntity() throws IOException { public boolean hasNextEntity() throws IOException {
//Entity has yet to be consumed. //Entity has yet to be consumed.
if (xryEntity.length() > 0) { if (xryEntity.length() > 0) {
return true; return true;
} }
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
if (marksEndOfEntity(line)) { if (marksEndOfEntity(line)) {
@ -98,9 +100,12 @@ public final class XRYFileReader {
} }
/** /**
* Returns an XRY entity if there is one, otherwise an exception is thrown.
* *
* @return * @return A non-empty XRY entity.
* @throws IOException * @throws IOException if an I/O error occurs.
* @throws NoSuchElementException if there are no more XRY entities to
* consume.
*/ */
public String nextEntity() throws IOException { public String nextEntity() throws IOException {
if (hasNextEntity()) { if (hasNextEntity()) {
@ -113,9 +118,11 @@ public final class XRYFileReader {
} }
/** /**
* Closes any file handles this reader may have open.
* *
* @throws IOException * @throws IOException
*/ */
@Override
public void close() throws IOException { public void close() throws IOException {
reader.close(); reader.close();
} }