Coverage Summary for Class: AppendableWriter (com.google.common.io)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| AppendableWriter | 0% (0/1) | 0% (0/11) | 0% (0/29) |
1 /* 2 * Copyright (C) 2006 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 19 import com.google.common.annotations.GwtIncompatible; 20 import java.io.Closeable; 21 import java.io.Flushable; 22 import java.io.IOException; 23 import java.io.Writer; 24 import javax.annotation.CheckForNull; 25 26 /** 27 * Writer that places all output on an {@link Appendable} target. If the target is {@link Flushable} 28 * or {@link Closeable}, flush()es and close()s will also be delegated to the target. 29 * 30 * @author Alan Green 31 * @author Sebastian Kanthak 32 * @since 1.0 33 */ 34 @GwtIncompatible 35 @ElementTypesAreNonnullByDefault 36 class AppendableWriter extends Writer { 37 private final Appendable target; 38 private boolean closed; 39 40 /** 41 * Creates a new writer that appends everything it writes to {@code target}. 42 * 43 * @param target target to which to append output 44 */ 45 AppendableWriter(Appendable target) { 46 this.target = checkNotNull(target); 47 } 48 49 /* 50 * Abstract methods from Writer 51 */ 52 53 @Override 54 public void write(char[] cbuf, int off, int len) throws IOException { 55 checkNotClosed(); 56 // It turns out that creating a new String is usually as fast, or faster 57 // than wrapping cbuf in a light-weight CharSequence. 58 target.append(new String(cbuf, off, len)); 59 } 60 61 /* 62 * Override a few functions for performance reasons to avoid creating unnecessary strings. 63 */ 64 65 @Override 66 public void write(int c) throws IOException { 67 checkNotClosed(); 68 target.append((char) c); 69 } 70 71 @Override 72 public void write(String str) throws IOException { 73 checkNotNull(str); 74 checkNotClosed(); 75 target.append(str); 76 } 77 78 @Override 79 public void write(String str, int off, int len) throws IOException { 80 checkNotNull(str); 81 checkNotClosed(); 82 // tricky: append takes start, end pair... 83 target.append(str, off, off + len); 84 } 85 86 @Override 87 public void flush() throws IOException { 88 checkNotClosed(); 89 if (target instanceof Flushable) { 90 ((Flushable) target).flush(); 91 } 92 } 93 94 @Override 95 public void close() throws IOException { 96 this.closed = true; 97 if (target instanceof Closeable) { 98 ((Closeable) target).close(); 99 } 100 } 101 102 @Override 103 public Writer append(char c) throws IOException { 104 checkNotClosed(); 105 target.append(c); 106 return this; 107 } 108 109 @Override 110 public Writer append(@CheckForNull CharSequence charSeq) throws IOException { 111 checkNotClosed(); 112 target.append(charSeq); 113 return this; 114 } 115 116 @Override 117 public Writer append(@CheckForNull CharSequence charSeq, int start, int end) throws IOException { 118 checkNotClosed(); 119 target.append(charSeq, start, end); 120 return this; 121 } 122 123 private void checkNotClosed() throws IOException { 124 if (closed) { 125 throw new IOException("Cannot write to a closed writer."); 126 } 127 } 128 }