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