Coverage Report - eu.simuline.octave.io.impl.AbstractOctaveStringReader
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractOctaveStringReader
80%
17/21
75%
9/12
10
 
 1  
 /*
 2  
  * Copyright 2008 Ange Optimization ApS
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package eu.simuline.octave.io.impl;
 17  
 
 18  
 import java.io.BufferedReader;
 19  
 
 20  
 import eu.simuline.octave.exception.OctaveParseException;
 21  
 import eu.simuline.octave.io.OctaveIO;
 22  
 import eu.simuline.octave.io.spi.OctaveDataReader;
 23  
 import eu.simuline.octave.type.OctaveString;
 24  
 
 25  
 /**
 26  
  * The reader of string/sq_string. 
 27  
  *
 28  
  * @see OctaveStringReader
 29  
  * @see OctaveSqStringReader
 30  
  */
 31  4
 public abstract class AbstractOctaveStringReader extends OctaveDataReader {
 32  
 
 33  
 
 34  
     private static final String H_ELEMENTS1 = "# elements: 1";
 35  
     private static final String H_ELEMENTS0 = "# elements: 0";
 36  
 
 37  
     private static final String LENGTH = "# length: ";
 38  
 
 39  
     /**
 40  
      * Overriding this method may add just handling of <code>\\</code> 
 41  
      * or delegate to this method. 
 42  
      */
 43  
     //@Override
 44  
     protected final OctaveString readImpl(final BufferedReader reader) {
 45  58
         final String elements = OctaveIO.readerReadLine(reader);
 46  58
         final StringBuilder builder = new StringBuilder();
 47  
         // NOTE: in java > 1.7 strings are allowed in switch 
 48  58
         if (H_ELEMENTS1.equals(elements)) {
 49  54
             final String lengthString = OctaveIO.readerReadLine(reader);
 50  54
             if (!lengthString.startsWith(LENGTH)) {
 51  0
                 throw new OctaveParseException
 52  
                     ("Parse error in String, line='" + lengthString + "'");
 53  
             }
 54  54
             final int length = Integer
 55  54
                 .parseInt(lengthString.substring(LENGTH.length()));
 56  
 
 57  54
             boolean first = true;
 58  134
             while (builder.length() < length) {
 59  80
                 if (!first) {
 60  28
                     builder.append('\n');
 61  
                 }
 62  80
                 builder.append(OctaveIO.readerReadLine(reader));
 63  80
                 first = false;
 64  
             }
 65  
 
 66  54
             if (builder.length() != length) {
 67  0
                 throw new OctaveParseException
 68  
                     ("Unexpected length of string read. expected=" + 
 69  0
                      length + ", actual=" + builder.length());
 70  
             }
 71  54
         } else if (!H_ELEMENTS0.equals(elements)) {
 72  0
             throw new OctaveParseException
 73  
                 ("Expected elements to be 0 or 1, '" + elements + "'");
 74  
         }
 75  58
         return new OctaveString(builder.toString());
 76  
     }
 77  
 }