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  
  * @since 1.4
 31  
  */
 32  
 public class CharSequenceReader extends Reader implements Serializable {
 33  
 
 34  
     private static final long serialVersionUID = 3724187752191401220L;
 35  
     private final CharSequence charSequence;
 36  
     private int idx;
 37  
     private int mark;
 38  
 
 39  
     /**
 40  
      * Construct a new instance with the specified character sequence.
 41  
      *
 42  
      * @param charSequence The character sequence, may be {@code null}
 43  
      */
 44  14
     public CharSequenceReader(final CharSequence charSequence) {
 45  14
         this.charSequence = charSequence != null ? charSequence : "";
 46  14
     }
 47  
 
 48  
     /**
 49  
      * Close resets the file back to the start and removes any marked position.
 50  
      */
 51  
     @Override
 52  
     public void close() {
 53  14
         idx = 0;
 54  14
         mark = 0;
 55  14
     }
 56  
 
 57  
     /**
 58  
      * Mark the current position.
 59  
      *
 60  
      * @param readAheadLimit ignored
 61  
      */
 62  
     @Override
 63  
     public void mark(final int readAheadLimit) {
 64  2
         mark = idx;
 65  2
     }
 66  
 
 67  
     /**
 68  
      * Mark is supported (returns true).
 69  
      *
 70  
      * @return {@code true}
 71  
      */
 72  
     @Override
 73  
     public boolean markSupported() {
 74  2
         return true;
 75  
     }
 76  
 
 77  
     /**
 78  
      * Read a single character.
 79  
      *
 80  
      * @return the next character from the character sequence
 81  
      * or -1 if the end has been reached.
 82  
      */
 83  
     @Override
 84  
     public int read() {
 85  86
         if (idx >= charSequence.length()) {
 86  8
             return EOF;
 87  
         } else {
 88  78
             return charSequence.charAt(idx++);
 89  
         }
 90  
     }
 91  
 
 92  
     /**
 93  
      * Read the specified number of characters into the array.
 94  
      *
 95  
      * @param array The array to store the characters in
 96  
      * @param offset The starting position in the array to store
 97  
      * @param length The maximum number of characters to read
 98  
      * @return The number of characters read or -1 if there are
 99  
      * no more
 100  
      */
 101  
     @Override
 102  
     public int read(final char[] array, final int offset, final int length) {
 103  14
         if (idx >= charSequence.length()) {
 104  4
             return EOF;
 105  
         }
 106  10
         if (array == null) {
 107  0
             throw new NullPointerException("Character array is missing");
 108  
         }
 109  10
         if (length < 0 || offset < 0 || offset + length > array.length) {
 110  0
             throw new IndexOutOfBoundsException("Array Size=" + array.length +
 111  
                     ", offset=" + offset + ", length=" + length);
 112  
         }
 113  10
         int count = 0;
 114  34
         for (int i = 0; i < length; i++) {
 115  26
             final int c = read();
 116  26
             if (c == EOF) {
 117  2
                 return count;
 118  
             }
 119  24
             array[offset + i] = (char)c;
 120  24
             count++;
 121  
         }
 122  8
         return count;
 123  
     }
 124  
 
 125  
     /**
 126  
      * Reset the reader to the last marked position (or the beginning if
 127  
      * mark has not been called).
 128  
      */
 129  
     @Override
 130  
     public void reset() {
 131  6
         idx = mark;
 132  6
     }
 133  
 
 134  
     /**
 135  
      * Skip the specified number of characters.
 136  
      *
 137  
      * @param n The number of characters to skip
 138  
      * @return The actual number of characters skipped
 139  
      */
 140  
     @Override
 141  
     public long skip(final long n) {
 142  12
         if (n < 0) {
 143  0
             throw new IllegalArgumentException(
 144  
                     "Number of characters to skip is less than zero: " + n);
 145  
         }
 146  12
         if (idx >= charSequence.length()) {
 147  4
             return EOF;
 148  
         }
 149  8
         final int dest = (int)Math.min(charSequence.length(), idx + n);
 150  8
         final int count = dest - idx;
 151  8
         idx = dest;
 152  8
         return count;
 153  
     }
 154  
 
 155  
     /**
 156  
      * Return a String representation of the underlying
 157  
      * character sequence.
 158  
      *
 159  
      * @return The contents of the character sequence
 160  
      */
 161  
     @Override
 162  
     public String toString() {
 163  0
         return charSequence.toString();
 164  
     }
 165  
 }