Coverage Report - net.sf.jmatchparser.util.LineCountVerifyReader
 
Classes in this File Line Coverage Branch Coverage Complexity
LineCountVerifyReader
89%
43/48
80%
24/30
5,2
 
 1  
 /*
 2  
  * Copyright (c) 2010 - 2011 Michael Schierl
 3  
  * 
 4  
  * All rights reserved.
 5  
  * 
 6  
  * Redistribution and use in source and binary forms, with or without
 7  
  * modification, are permitted provided that the following conditions
 8  
  * are met:
 9  
  * 
 10  
  * - Redistributions of source code must retain the above copyright notice,
 11  
  *   this list of conditions and the following disclaimer.
 12  
  *   
 13  
  * - Redistributions in binary form must reproduce the above copyright
 14  
  *   notice, this list of conditions and the following disclaimer in the
 15  
  *   documentation and/or other materials provided with the distribution.
 16  
  *   
 17  
  * - Neither name of the copyright holders nor the names of its
 18  
  *   contributors may be used to endorse or promote products derived from
 19  
  *   this software without specific prior written permission.
 20  
  *   
 21  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND THE CONTRIBUTORS
 22  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 23  
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 24  
  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 25  
  * HOLDERS OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 26  
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 27  
  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 28  
  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 29  
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 30  
  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 31  
  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32  
  */
 33  
 package net.sf.jmatchparser.util;
 34  
 
 35  
 import java.io.BufferedReader;
 36  
 import java.io.IOException;
 37  
 import java.io.Reader;
 38  
 import java.util.regex.Matcher;
 39  
 import java.util.regex.Pattern;
 40  
 
 41  
 /**
 42  
  * A {@link Reader} that reads from a {@link BufferedReader} and counts the
 43  
  * lines read. At the end of the output, there is a marker followed by the
 44  
  * actual line count. If the line count is wrong, an exception is thrown, else
 45  
  * the line count and everything after it is ignored.
 46  
  * 
 47  
  * As a convenience, when closing a not fully consumed reader, the remaining
 48  
  * lines are read and the line count is verified. Therefore, to verify the line
 49  
  * count without reading the contents, it is sufficient to close the reader.
 50  
  */
 51  
 public class LineCountVerifyReader extends Reader {
 52  
 
 53  
         private final BufferedReader br;
 54  8
         private String currentLine = null;
 55  
         private final Pattern lineCountMarkerPattern;
 56  
         private final Pattern lineCountPattern;
 57  
         private int counter;
 58  
         private boolean counterFound;
 59  
 
 60  
         /**
 61  
          * Create a new reader.
 62  
          * 
 63  
          * @param br
 64  
          *            Buffered reader to read from
 65  
          * @param lineCountMarkerPattern
 66  
          *            Pattern of the line that marks the end of the file and the
 67  
          *            following line count
 68  
          * @param lineCountPattern
 69  
          *            Pattern of the line that contains the line count as group 1.
 70  
          *            If <code>null</code> group 1 of the previous pattern contains
 71  
          *            the line count
 72  
          * @param lineCountOffset
 73  
          *            Difference between number of lines and line count printed.
 74  
          *            Positive if the printed line count is higher and vice versa
 75  
          */
 76  8
         public LineCountVerifyReader(BufferedReader br, Pattern lineCountMarkerPattern, Pattern lineCountPattern, int lineCountOffset) {
 77  8
                 this.br = br;
 78  8
                 this.lineCountMarkerPattern = lineCountMarkerPattern;
 79  8
                 this.lineCountPattern = lineCountPattern;
 80  8
                 counter = lineCountOffset;
 81  8
         }
 82  
 
 83  
         @Override
 84  
         public boolean ready() throws IOException {
 85  0
                 return currentLine != null || br.ready();
 86  
         }
 87  
 
 88  
         @Override
 89  
         public int read(char[] cbuf, int off, int len) throws IOException {
 90  33
                 ensureFillCurrentLine();
 91  28
                 if (currentLine == null) {
 92  5
                         return -1;
 93  
                 }
 94  23
                 if (len <= currentLine.length()) {
 95  0
                         System.arraycopy(currentLine.toCharArray(), 0, cbuf, off, len);
 96  0
                         currentLine = currentLine.substring(len);
 97  0
                         return len;
 98  
                 }
 99  23
                 int result = currentLine.length() + 1;
 100  23
                 System.arraycopy(currentLine.toCharArray(), 0, cbuf, off, currentLine.length());
 101  23
                 cbuf[off + currentLine.length()] = '\n';
 102  23
                 currentLine = null;
 103  23
                 return result;
 104  
         }
 105  
 
 106  
         @Override
 107  
         public void close() throws IOException {
 108  7
                 char[] buf = new char[256];
 109  22
                 while (read(buf) != -1)
 110  15
                         ;
 111  3
                 br.close();
 112  3
         }
 113  
 
 114  
         private void ensureFillCurrentLine() throws IOException {
 115  33
                 if (currentLine != null)
 116  0
                         return;
 117  33
                 currentLine = br.readLine();
 118  33
                 if (currentLine == null) {
 119  3
                         if (!counterFound)
 120  1
                                 throw new IOException("File record count marker line missing");
 121  2
                         return;
 122  
                 }
 123  30
                 Matcher m = lineCountMarkerPattern.matcher(currentLine);
 124  30
                 if (m.matches()) {
 125  7
                         if (lineCountPattern != null) {
 126  3
                                 currentLine = br.readLine();
 127  3
                                 m = currentLine == null ? null : lineCountPattern.matcher(currentLine);
 128  3
                                 if (m == null || !m.matches()) {
 129  2
                                         throw new IOException("File record count line is invalid: " + currentLine);
 130  
                                 }
 131  
                         }
 132  5
                         int fileCounter = Integer.parseInt(m.group(1));
 133  5
                         if (fileCounter != counter)
 134  2
                                 throw new IOException("File count incorrect: " + fileCounter + " != " + counter);
 135  6
                         while (br.readLine() != null)
 136  3
                                 ;
 137  3
                         currentLine = null;
 138  3
                         counterFound = true;
 139  
                 }
 140  26
                 counter++;
 141  26
         }
 142  
 }