001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.io; 016 017import static com.google.common.base.Preconditions.checkNotNull; 018import static com.google.common.base.Preconditions.checkPositionIndexes; 019 020import com.google.common.annotations.Beta; 021import com.google.common.annotations.GwtIncompatible; 022 023import java.io.Closeable; 024import java.io.EOFException; 025import java.io.IOException; 026import java.io.Reader; 027import java.io.Writer; 028import java.nio.Buffer; 029import java.nio.CharBuffer; 030import java.util.ArrayList; 031import java.util.List; 032 033/** 034 * Provides utility methods for working with character streams. 035 * 036 * <p>All method parameters must be non-null unless documented otherwise. 037 * 038 * <p>Some of the methods in this class take arguments with a generic type of {@code Readable & 039 * Closeable}. A {@link java.io.Reader} implements both of those interfaces. Similarly for {@code 040 * Appendable & Closeable} and {@link java.io.Writer}. 041 * 042 * @author Chris Nokleberg 043 * @author Bin Zhu 044 * @author Colin Decker 045 * @since 1.0 046 */ 047@Beta 048@GwtIncompatible 049public final class CharStreams { 050 051 // 2K chars (4K bytes) 052 private static final int DEFAULT_BUF_SIZE = 0x800; 053 054 /** Creates a new {@code CharBuffer} for buffering reads or writes. */ 055 static CharBuffer createBuffer() { 056 return CharBuffer.allocate(DEFAULT_BUF_SIZE); 057 } 058 059 private CharStreams() {} 060 061 /** 062 * Copies all characters between the {@link Readable} and {@link Appendable} objects. Does not 063 * close or flush either object. 064 * 065 * @param from the object to read from 066 * @param to the object to write to 067 * @return the number of characters copied 068 * @throws IOException if an I/O error occurs 069 */ 070 071 public static long copy(Readable from, Appendable to) throws IOException { 072 // The most common case is that from is a Reader (like InputStreamReader or StringReader) so 073 // take advantage of that. 074 if (from instanceof Reader) { 075 // optimize for common output types which are optimized to deal with char[] 076 if (to instanceof StringBuilder) { 077 return copyReaderToBuilder((Reader) from, (StringBuilder) to); 078 } else { 079 return copyReaderToWriter((Reader) from, asWriter(to)); 080 } 081 } else { 082 checkNotNull(from); 083 checkNotNull(to); 084 long total = 0; 085 CharBuffer buf = createBuffer(); 086 while (from.read(buf) != -1) { 087 ((Buffer)buf).flip(); 088 to.append(buf); 089 total += buf.remaining(); 090 ((Buffer)buf).clear(); 091 } 092 return total; 093 } 094 } 095 096 // TODO(lukes): consider allowing callers to pass in a buffer to use, some callers would be able 097 // to reuse buffers, others would be able to size them more appropriately than the constant 098 // defaults 099 100 /** 101 * Copies all characters between the {@link Reader} and {@link StringBuilder} objects. Does not 102 * close or flush the reader. 103 * 104 * <p>This is identical to {@link #copy(Readable, Appendable)} but optimized for these specific 105 * types. CharBuffer has poor performance when being written into or read out of so round tripping 106 * all the bytes through the buffer takes a long time. With these specialized types we can just 107 * use a char array. 108 * 109 * @param from the object to read from 110 * @param to the object to write to 111 * @return the number of characters copied 112 * @throws IOException if an I/O error occurs 113 */ 114 115 static long copyReaderToBuilder(Reader from, StringBuilder to) throws IOException { 116 checkNotNull(from); 117 checkNotNull(to); 118 char[] buf = new char[DEFAULT_BUF_SIZE]; 119 int nRead; 120 long total = 0; 121 while ((nRead = from.read(buf)) != -1) { 122 to.append(buf, 0, nRead); 123 total += nRead; 124 } 125 return total; 126 } 127 128 /** 129 * Copies all characters between the {@link Reader} and {@link Writer} objects. Does not close or 130 * flush the reader or writer. 131 * 132 * <p>This is identical to {@link #copy(Readable, Appendable)} but optimized for these specific 133 * types. CharBuffer has poor performance when being written into or read out of so round tripping 134 * all the bytes through the buffer takes a long time. With these specialized types we can just 135 * use a char array. 136 * 137 * @param from the object to read from 138 * @param to the object to write to 139 * @return the number of characters copied 140 * @throws IOException if an I/O error occurs 141 */ 142 143 static long copyReaderToWriter(Reader from, Writer to) throws IOException { 144 checkNotNull(from); 145 checkNotNull(to); 146 char[] buf = new char[DEFAULT_BUF_SIZE]; 147 int nRead; 148 long total = 0; 149 while ((nRead = from.read(buf)) != -1) { 150 to.write(buf, 0, nRead); 151 total += nRead; 152 } 153 return total; 154 } 155 156 /** 157 * Reads all characters from a {@link Readable} object into a {@link String}. Does not close the 158 * {@code Readable}. 159 * 160 * @param r the object to read from 161 * @return a string containing all the characters 162 * @throws IOException if an I/O error occurs 163 */ 164 public static String toString(Readable r) throws IOException { 165 return toStringBuilder(r).toString(); 166 } 167 168 /** 169 * Reads all characters from a {@link Readable} object into a new {@link StringBuilder} instance. 170 * Does not close the {@code Readable}. 171 * 172 * @param r the object to read from 173 * @return a {@link StringBuilder} containing all the characters 174 * @throws IOException if an I/O error occurs 175 */ 176 private static StringBuilder toStringBuilder(Readable r) throws IOException { 177 StringBuilder sb = new StringBuilder(); 178 if (r instanceof Reader) { 179 copyReaderToBuilder((Reader) r, sb); 180 } else { 181 copy(r, sb); 182 } 183 return sb; 184 } 185 186 /** 187 * Reads all of the lines from a {@link Readable} object. The lines do not include 188 * line-termination characters, but do include other leading and trailing whitespace. 189 * 190 * <p>Does not close the {@code Readable}. If reading files or resources you should use the {@link 191 * Files#readLines} and {@link Resources#readLines} methods. 192 * 193 * @param r the object to read from 194 * @return a mutable {@link List} containing all the lines 195 * @throws IOException if an I/O error occurs 196 */ 197 public static List<String> readLines(Readable r) throws IOException { 198 List<String> result = new ArrayList<>(); 199 LineReader lineReader = new LineReader(r); 200 String line; 201 while ((line = lineReader.readLine()) != null) { 202 result.add(line); 203 } 204 return result; 205 } 206 207 /** 208 * Streams lines from a {@link Readable} object, stopping when the processor returns {@code false} 209 * or all lines have been read and returning the result produced by the processor. Does not close 210 * {@code readable}. Note that this method may not fully consume the contents of {@code readable} 211 * if the processor stops processing early. 212 * 213 * @throws IOException if an I/O error occurs 214 * @since 14.0 215 */ 216 // some processors won't return a useful result 217 public static <T> T readLines(Readable readable, LineProcessor<T> processor) throws IOException { 218 checkNotNull(readable); 219 checkNotNull(processor); 220 221 LineReader lineReader = new LineReader(readable); 222 String line; 223 while ((line = lineReader.readLine()) != null) { 224 if (!processor.processLine(line)) { 225 break; 226 } 227 } 228 return processor.getResult(); 229 } 230 231 /** 232 * Reads and discards data from the given {@code Readable} until the end of the stream is reached. 233 * Returns the total number of chars read. Does not close the stream. 234 * 235 * @since 20.0 236 */ 237 238 public static long exhaust(Readable readable) throws IOException { 239 long total = 0; 240 long read; 241 CharBuffer buf = createBuffer(); 242 while ((read = readable.read(buf)) != -1) { 243 total += read; 244 ((Buffer)buf).clear(); 245 } 246 return total; 247 } 248 249 /** 250 * Discards {@code n} characters of data from the reader. This method will block until the full 251 * amount has been skipped. Does not close the reader. 252 * 253 * @param reader the reader to read from 254 * @param n the number of characters to skip 255 * @throws EOFException if this stream reaches the end before skipping all the characters 256 * @throws IOException if an I/O error occurs 257 */ 258 public static void skipFully(Reader reader, long n) throws IOException { 259 checkNotNull(reader); 260 while (n > 0) { 261 long amt = reader.skip(n); 262 if (amt == 0) { 263 throw new EOFException(); 264 } 265 n -= amt; 266 } 267 } 268 269 /** 270 * Returns a {@link Writer} that simply discards written chars. 271 * 272 * @since 15.0 273 */ 274 public static Writer nullWriter() { 275 return NullWriter.INSTANCE; 276 } 277 278 private static final class NullWriter extends Writer { 279 280 private static final NullWriter INSTANCE = new NullWriter(); 281 282 @Override 283 public void write(int c) {} 284 285 @Override 286 public void write(char[] cbuf) { 287 checkNotNull(cbuf); 288 } 289 290 @Override 291 public void write(char[] cbuf, int off, int len) { 292 checkPositionIndexes(off, off + len, cbuf.length); 293 } 294 295 @Override 296 public void write(String str) { 297 checkNotNull(str); 298 } 299 300 @Override 301 public void write(String str, int off, int len) { 302 checkPositionIndexes(off, off + len, str.length()); 303 } 304 305 @Override 306 public Writer append(CharSequence csq) { 307 checkNotNull(csq); 308 return this; 309 } 310 311 @Override 312 public Writer append(CharSequence csq, int start, int end) { 313 checkPositionIndexes(start, end, csq.length()); 314 return this; 315 } 316 317 @Override 318 public Writer append(char c) { 319 return this; 320 } 321 322 @Override 323 public void flush() {} 324 325 @Override 326 public void close() {} 327 328 @Override 329 public String toString() { 330 return "CharStreams.nullWriter()"; 331 } 332 } 333 334 /** 335 * Returns a Writer that sends all output to the given {@link Appendable} target. Closing the 336 * writer will close the target if it is {@link Closeable}, and flushing the writer will flush the 337 * target if it is {@link java.io.Flushable}. 338 * 339 * @param target the object to which output will be sent 340 * @return a new Writer object, unless target is a Writer, in which case the target is returned 341 */ 342 public static Writer asWriter(Appendable target) { 343 if (target instanceof Writer) { 344 return (Writer) target; 345 } 346 return new AppendableWriter(target); 347 } 348}