Coverage Summary for Class: CharStreams (com.google.common.io)

Class Method, % Line, %
CharStreams 0% (0/13) 0% (0/67)
CharStreams$NullWriter 0% (0/10) 0% (0/11)
Total 0% (0/23) 0% (0/78)


1 /* 2  * Copyright (C) 2007 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5  * in compliance with the License. You may obtain a copy of the License at 6  * 7  * http://www.apache.org/licenses/LICENSE-2.0 8  * 9  * Unless required by applicable law or agreed to in writing, software distributed under the License 10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11  * or implied. See the License for the specific language governing permissions and limitations under 12  * the License. 13  */ 14  15 package com.google.common.io; 16  17 import static com.google.common.base.Preconditions.checkNotNull; 18 import static com.google.common.base.Preconditions.checkPositionIndexes; 19  20 import com.google.common.annotations.Beta; 21 import com.google.common.annotations.GwtIncompatible; 22 import com.google.errorprone.annotations.CanIgnoreReturnValue; 23 import java.io.Closeable; 24 import java.io.EOFException; 25 import java.io.IOException; 26 import java.io.Reader; 27 import java.io.Writer; 28 import java.nio.CharBuffer; 29 import java.util.ArrayList; 30 import java.util.List; 31 import javax.annotation.CheckForNull; 32 import org.checkerframework.checker.nullness.qual.Nullable; 33  34 /** 35  * Provides utility methods for working with character streams. 36  * 37  * <p>All method parameters must be non-null unless documented otherwise. 38  * 39  * <p>Some of the methods in this class take arguments with a generic type of {@code Readable & 40  * Closeable}. A {@link java.io.Reader} implements both of those interfaces. Similarly for {@code 41  * Appendable & Closeable} and {@link java.io.Writer}. 42  * 43  * @author Chris Nokleberg 44  * @author Bin Zhu 45  * @author Colin Decker 46  * @since 1.0 47  */ 48 @GwtIncompatible 49 @ElementTypesAreNonnullByDefault 50 public final class CharStreams { 51  52  // 2K chars (4K bytes) 53  private static final int DEFAULT_BUF_SIZE = 0x800; 54  55  /** Creates a new {@code CharBuffer} for buffering reads or writes. */ 56  static CharBuffer createBuffer() { 57  return CharBuffer.allocate(DEFAULT_BUF_SIZE); 58  } 59  60  private CharStreams() {} 61  62  /** 63  * Copies all characters between the {@link Readable} and {@link Appendable} objects. Does not 64  * close or flush either object. 65  * 66  * @param from the object to read from 67  * @param to the object to write to 68  * @return the number of characters copied 69  * @throws IOException if an I/O error occurs 70  */ 71  @CanIgnoreReturnValue 72  public static long copy(Readable from, Appendable to) throws IOException { 73  // The most common case is that from is a Reader (like InputStreamReader or StringReader) so 74  // take advantage of that. 75  if (from instanceof Reader) { 76  // optimize for common output types which are optimized to deal with char[] 77  if (to instanceof StringBuilder) { 78  return copyReaderToBuilder((Reader) from, (StringBuilder) to); 79  } else { 80  return copyReaderToWriter((Reader) from, asWriter(to)); 81  } 82  } 83  84  checkNotNull(from); 85  checkNotNull(to); 86  long total = 0; 87  CharBuffer buf = createBuffer(); 88  while (from.read(buf) != -1) { 89  Java8Compatibility.flip(buf); 90  to.append(buf); 91  total += buf.remaining(); 92  Java8Compatibility.clear(buf); 93  } 94  return total; 95  } 96  97  // TODO(lukes): consider allowing callers to pass in a buffer to use, some callers would be able 98  // to reuse buffers, others would be able to size them more appropriately than the constant 99  // defaults 100  101  /** 102  * Copies all characters between the {@link Reader} and {@link StringBuilder} objects. Does not 103  * close or flush the reader. 104  * 105  * <p>This is identical to {@link #copy(Readable, Appendable)} but optimized for these specific 106  * types. CharBuffer has poor performance when being written into or read out of so round tripping 107  * all the bytes through the buffer takes a long time. With these specialized types we can just 108  * use a char array. 109  * 110  * @param from the object to read from 111  * @param to the object to write to 112  * @return the number of characters copied 113  * @throws IOException if an I/O error occurs 114  */ 115  @CanIgnoreReturnValue 116  static long copyReaderToBuilder(Reader from, StringBuilder to) throws IOException { 117  checkNotNull(from); 118  checkNotNull(to); 119  char[] buf = new char[DEFAULT_BUF_SIZE]; 120  int nRead; 121  long total = 0; 122  while ((nRead = from.read(buf)) != -1) { 123  to.append(buf, 0, nRead); 124  total += nRead; 125  } 126  return total; 127  } 128  129  /** 130  * Copies all characters between the {@link Reader} and {@link Writer} objects. Does not close or 131  * flush the reader or writer. 132  * 133  * <p>This is identical to {@link #copy(Readable, Appendable)} but optimized for these specific 134  * types. CharBuffer has poor performance when being written into or read out of so round tripping 135  * all the bytes through the buffer takes a long time. With these specialized types we can just 136  * use a char array. 137  * 138  * @param from the object to read from 139  * @param to the object to write to 140  * @return the number of characters copied 141  * @throws IOException if an I/O error occurs 142  */ 143  @CanIgnoreReturnValue 144  static long copyReaderToWriter(Reader from, Writer to) throws IOException { 145  checkNotNull(from); 146  checkNotNull(to); 147  char[] buf = new char[DEFAULT_BUF_SIZE]; 148  int nRead; 149  long total = 0; 150  while ((nRead = from.read(buf)) != -1) { 151  to.write(buf, 0, nRead); 152  total += nRead; 153  } 154  return total; 155  } 156  157  /** 158  * Reads all characters from a {@link Readable} object into a {@link String}. Does not close the 159  * {@code Readable}. 160  * 161  * @param r the object to read from 162  * @return a string containing all the characters 163  * @throws IOException if an I/O error occurs 164  */ 165  public static String toString(Readable r) throws IOException { 166  return toStringBuilder(r).toString(); 167  } 168  169  /** 170  * Reads all characters from a {@link Readable} object into a new {@link StringBuilder} instance. 171  * Does not close the {@code Readable}. 172  * 173  * @param r the object to read from 174  * @return a {@link StringBuilder} containing all the characters 175  * @throws IOException if an I/O error occurs 176  */ 177  private static StringBuilder toStringBuilder(Readable r) throws IOException { 178  StringBuilder sb = new StringBuilder(); 179  if (r instanceof Reader) { 180  copyReaderToBuilder((Reader) r, sb); 181  } else { 182  copy(r, sb); 183  } 184  return sb; 185  } 186  187  /** 188  * Reads all of the lines from a {@link Readable} object. The lines do not include 189  * line-termination characters, but do include other leading and trailing whitespace. 190  * 191  * <p>Does not close the {@code Readable}. If reading files or resources you should use the {@link 192  * Files#readLines} and {@link Resources#readLines} methods. 193  * 194  * @param r the object to read from 195  * @return a mutable {@link List} containing all the lines 196  * @throws IOException if an I/O error occurs 197  */ 198  @Beta 199  public static List<String> readLines(Readable r) throws IOException { 200  List<String> result = new ArrayList<>(); 201  LineReader lineReader = new LineReader(r); 202  String line; 203  while ((line = lineReader.readLine()) != null) { 204  result.add(line); 205  } 206  return result; 207  } 208  209  /** 210  * Streams lines from a {@link Readable} object, stopping when the processor returns {@code false} 211  * or all lines have been read and returning the result produced by the processor. Does not close 212  * {@code readable}. Note that this method may not fully consume the contents of {@code readable} 213  * if the processor stops processing early. 214  * 215  * @throws IOException if an I/O error occurs 216  * @since 14.0 217  */ 218  @Beta 219  @CanIgnoreReturnValue // some processors won't return a useful result 220  @ParametricNullness 221  public static <T extends @Nullable Object> T readLines( 222  Readable readable, LineProcessor<T> processor) throws IOException { 223  checkNotNull(readable); 224  checkNotNull(processor); 225  226  LineReader lineReader = new LineReader(readable); 227  String line; 228  while ((line = lineReader.readLine()) != null) { 229  if (!processor.processLine(line)) { 230  break; 231  } 232  } 233  return processor.getResult(); 234  } 235  236  /** 237  * Reads and discards data from the given {@code Readable} until the end of the stream is reached. 238  * Returns the total number of chars read. Does not close the stream. 239  * 240  * @since 20.0 241  */ 242  @Beta 243  @CanIgnoreReturnValue 244  public static long exhaust(Readable readable) throws IOException { 245  long total = 0; 246  long read; 247  CharBuffer buf = createBuffer(); 248  while ((read = readable.read(buf)) != -1) { 249  total += read; 250  Java8Compatibility.clear(buf); 251  } 252  return total; 253  } 254  255  /** 256  * Discards {@code n} characters of data from the reader. This method will block until the full 257  * amount has been skipped. Does not close the reader. 258  * 259  * @param reader the reader to read from 260  * @param n the number of characters to skip 261  * @throws EOFException if this stream reaches the end before skipping all the characters 262  * @throws IOException if an I/O error occurs 263  */ 264  @Beta 265  public static void skipFully(Reader reader, long n) throws IOException { 266  checkNotNull(reader); 267  while (n > 0) { 268  long amt = reader.skip(n); 269  if (amt == 0) { 270  throw new EOFException(); 271  } 272  n -= amt; 273  } 274  } 275  276  /** 277  * Returns a {@link Writer} that simply discards written chars. 278  * 279  * @since 15.0 280  */ 281  @Beta 282  public static Writer nullWriter() { 283  return NullWriter.INSTANCE; 284  } 285  286  private static final class NullWriter extends Writer { 287  288  private static final NullWriter INSTANCE = new NullWriter(); 289  290  @Override 291  public void write(int c) {} 292  293  @Override 294  public void write(char[] cbuf) { 295  checkNotNull(cbuf); 296  } 297  298  @Override 299  public void write(char[] cbuf, int off, int len) { 300  checkPositionIndexes(off, off + len, cbuf.length); 301  } 302  303  @Override 304  public void write(String str) { 305  checkNotNull(str); 306  } 307  308  @Override 309  public void write(String str, int off, int len) { 310  checkPositionIndexes(off, off + len, str.length()); 311  } 312  313  @Override 314  public Writer append(@CheckForNull CharSequence csq) { 315  return this; 316  } 317  318  @Override 319  public Writer append(@CheckForNull CharSequence csq, int start, int end) { 320  checkPositionIndexes(start, end, csq == null ? "null".length() : csq.length()); 321  return this; 322  } 323  324  @Override 325  public Writer append(char c) { 326  return this; 327  } 328  329  @Override 330  public void flush() {} 331  332  @Override 333  public void close() {} 334  335  @Override 336  public String toString() { 337  return "CharStreams.nullWriter()"; 338  } 339  } 340  341  /** 342  * Returns a Writer that sends all output to the given {@link Appendable} target. Closing the 343  * writer will close the target if it is {@link Closeable}, and flushing the writer will flush the 344  * target if it is {@link java.io.Flushable}. 345  * 346  * @param target the object to which output will be sent 347  * @return a new Writer object, unless target is a Writer, in which case the target is returned 348  */ 349  @Beta 350  public static Writer asWriter(Appendable target) { 351  if (target instanceof Writer) { 352  return (Writer) target; 353  } 354  return new AppendableWriter(target); 355  } 356 }