Coverage Report - org.apache.commons.io.input.CharSequenceReader
 
Classes in this File Line Coverage Branch Coverage Complexity
CharSequenceReader
89%
33/37
72%
16/22
3
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.io.input;
 18  
 
 19  
 import static org.apache.commons.io.IOUtils.EOF;
 20  
 
 21  
 import java.io.Reader;
 22  
 import java.io.Serializable;
 23  
 
 24  
 /**
 25  
  * {@link Reader} implementation that can read from String, StringBuffer,
 26  
  * StringBuilder or CharBuffer.
 27  
  * <p>
 28  
  * <strong>Note:</strong> Supports {@link #mark(int)} and {@link #reset()}.
 29  
  *
 30  
  * @version $Id: CharSequenceReader.java 1642757 2014-12-01 21:09:30Z sebb $
 31  
  * @since 1.4
 32  
  */
 33  
 public class CharSequenceReader extends Reader implements Serializable {
 34  
 
 35  
     private static final long serialVersionUID = 3724187752191401220L;
 36  
     private final CharSequence charSequence;
 37  
     private int idx;
 38  
     private int mark;
 39  
 
 40  
     /**
 41  
      * Construct a new instance with the specified character sequence.
 42  
      *
 43  
      * @param charSequence The character sequence, may be {@code null}
 44  
      */
 45  7
     public CharSequenceReader(final CharSequence charSequence) {
 46  7
         this.charSequence = charSequence != null ? charSequence : "";
 47  7
     }
 48  
 
 49  
     /**
 50  
      * Close resets the file back to the start and removes any marked position.
 51  
      */
 52  
     @Override
 53  
     public void close() {
 54  7
         idx = 0;
 55  7
         mark = 0;
 56  7
     }
 57  
 
 58  
     /**
 59  
      * Mark the current position.
 60  
      *
 61  
      * @param readAheadLimit ignored
 62  
      */
 63  
     @Override
 64  
     public void mark(final int readAheadLimit) {
 65  1
         mark = idx;
 66  1
     }
 67  
 
 68  
     /**
 69  
      * Mark is supported (returns true).
 70  
      *
 71  
      * @return {@code true}
 72  
      */
 73  
     @Override
 74  
     public boolean markSupported() {
 75  1
         return true;
 76  
     }
 77  
 
 78  
     /**
 79  
      * Read a single character.
 80  
      *
 81  
      * @return the next character from the character sequence
 82  
      * or -1 if the end has been reached.
 83  
      */
 84  
     @Override
 85  
     public int read() {
 86  43
         if (idx >= charSequence.length()) {
 87  4
             return EOF;
 88  
         } else {
 89  39
             return charSequence.charAt(idx++);
 90  
         }
 91  
     }
 92  
 
 93  
     /**
 94  
      * Read the sepcified number of characters into the array.
 95  
      *
 96  
      * @param array The array to store the characters in
 97  
      * @param offset The starting position in the array to store
 98  
      * @param length The maximum number of characters to read
 99  
      * @return The number of characters read or -1 if there are
 100  
      * no more
 101  
      */
 102  
     @Override
 103  
     public int read(final char[] array, final int offset, final int length) {
 104  7
         if (idx >= charSequence.length()) {
 105  2
             return EOF;
 106  
         }
 107  5
         if (array == null) {
 108  0
             throw new NullPointerException("Character array is missing");
 109  
         }
 110  5
         if (length < 0 || offset < 0 || offset + length > array.length) {
 111  0
             throw new IndexOutOfBoundsException("Array Size=" + array.length +
 112  
                     ", offset=" + offset + ", length=" + length);
 113  
         }
 114  5
         int count = 0;
 115  17
         for (int i = 0; i < length; i++) {
 116  13
             final int c = read();
 117  13
             if (c == EOF) {
 118  1
                 return count;
 119  
             }
 120  12
             array[offset + i] = (char)c;
 121  12
             count++;
 122  
         }
 123  4
         return count;
 124  
     }
 125  
 
 126  
     /**
 127  
      * Reset the reader to the last marked position (or the beginning if
 128  
      * mark has not been called).
 129  
      */
 130  
     @Override
 131  
     public void reset() {
 132  3
         idx = mark;
 133  3
     }
 134  
 
 135  
     /**
 136  
      * Skip the specified number of characters.
 137  
      *
 138  
      * @param n The number of characters to skip
 139  
      * @return The actual number of characters skipped
 140  
      */
 141  
     @Override
 142  
     public long skip(final long n) {
 143  6
         if (n < 0) {
 144  0
             throw new IllegalArgumentException(
 145  
                     "Number of characters to skip is less than zero: " + n);
 146  
         }
 147  6
         if (idx >= charSequence.length()) {
 148  2
             return EOF;
 149  
         }
 150  4
         final int dest = (int)Math.min(charSequence.length(), idx + n);
 151  4
         final int count = dest - idx;
 152  4
         idx = dest;
 153  4
         return count;
 154  
     }
 155  
 
 156  
     /**
 157  
      * Return a String representation of the underlying
 158  
      * character sequence.
 159  
      *
 160  
      * @return The contents of the character sequence
 161  
      */
 162  
     @Override
 163  
     public String toString() {
 164  0
         return charSequence.toString();
 165  
     }
 166  
 }