| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ProxyWriter |
|
| 2.0;2 |
| 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.output; | |
| 18 | ||
| 19 | import java.io.FilterWriter; | |
| 20 | import java.io.IOException; | |
| 21 | import java.io.Writer; | |
| 22 | ||
| 23 | /** | |
| 24 | * A Proxy stream which acts as expected, that is it passes the method | |
| 25 | * calls on to the proxied stream and doesn't change which methods are | |
| 26 | * being called. It is an alternative base class to FilterWriter | |
| 27 | * to increase reusability, because FilterWriter changes the | |
| 28 | * methods being called, such as write(char[]) to write(char[], int, int) | |
| 29 | * and write(String) to write(String, int, int). | |
| 30 | * | |
| 31 | * @version $Id: ProxyWriter.java 1415850 2012-11-30 20:51:39Z ggregory $ | |
| 32 | */ | |
| 33 | 0 | public class ProxyWriter extends FilterWriter { |
| 34 | ||
| 35 | /** | |
| 36 | * Constructs a new ProxyWriter. | |
| 37 | * | |
| 38 | * @param proxy the Writer to delegate to | |
| 39 | */ | |
| 40 | public ProxyWriter(final Writer proxy) { | |
| 41 | 8 | super(proxy); |
| 42 | // the proxy is stored in a protected superclass variable named 'out' | |
| 43 | 8 | } |
| 44 | ||
| 45 | /** | |
| 46 | * Invokes the delegate's <code>append(char)</code> method. | |
| 47 | * @param c The character to write | |
| 48 | * @return this writer | |
| 49 | * @throws IOException if an I/O error occurs | |
| 50 | * @since 2.0 | |
| 51 | */ | |
| 52 | @Override | |
| 53 | public Writer append(final char c) throws IOException { | |
| 54 | try { | |
| 55 | 0 | beforeWrite(1); |
| 56 | 0 | out.append(c); |
| 57 | 0 | afterWrite(1); |
| 58 | 0 | } catch (final IOException e) { |
| 59 | 0 | handleIOException(e); |
| 60 | 0 | } |
| 61 | 0 | return this; |
| 62 | } | |
| 63 | ||
| 64 | /** | |
| 65 | * Invokes the delegate's <code>append(CharSequence, int, int)</code> method. | |
| 66 | * @param csq The character sequence to write | |
| 67 | * @param start The index of the first character to write | |
| 68 | * @param end The index of the first character to write (exclusive) | |
| 69 | * @return this writer | |
| 70 | * @throws IOException if an I/O error occurs | |
| 71 | * @since 2.0 | |
| 72 | */ | |
| 73 | @Override | |
| 74 | public Writer append(final CharSequence csq, final int start, final int end) throws IOException { | |
| 75 | try { | |
| 76 | 0 | beforeWrite(end - start); |
| 77 | 0 | out.append(csq, start, end); |
| 78 | 0 | afterWrite(end - start); |
| 79 | 0 | } catch (final IOException e) { |
| 80 | 0 | handleIOException(e); |
| 81 | 0 | } |
| 82 | 0 | return this; |
| 83 | } | |
| 84 | ||
| 85 | /** | |
| 86 | * Invokes the delegate's <code>append(CharSequence)</code> method. | |
| 87 | * @param csq The character sequence to write | |
| 88 | * @return this writer | |
| 89 | * @throws IOException if an I/O error occurs | |
| 90 | * @since 2.0 | |
| 91 | */ | |
| 92 | @Override | |
| 93 | public Writer append(final CharSequence csq) throws IOException { | |
| 94 | try { | |
| 95 | 2 | int len = 0; |
| 96 | 2 | if (csq != null) { |
| 97 | 1 | len = csq.length(); |
| 98 | } | |
| 99 | ||
| 100 | 2 | beforeWrite(len); |
| 101 | 2 | out.append(csq); |
| 102 | 2 | afterWrite(len); |
| 103 | 0 | } catch (final IOException e) { |
| 104 | 0 | handleIOException(e); |
| 105 | 2 | } |
| 106 | 2 | return this; |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * Invokes the delegate's <code>write(int)</code> method. | |
| 111 | * @param idx the character to write | |
| 112 | * @throws IOException if an I/O error occurs | |
| 113 | */ | |
| 114 | @Override | |
| 115 | public void write(final int idx) throws IOException { | |
| 116 | try { | |
| 117 | 0 | beforeWrite(1); |
| 118 | 0 | out.write(idx); |
| 119 | 0 | afterWrite(1); |
| 120 | 0 | } catch (final IOException e) { |
| 121 | 0 | handleIOException(e); |
| 122 | 0 | } |
| 123 | 0 | } |
| 124 | ||
| 125 | /** | |
| 126 | * Invokes the delegate's <code>write(char[])</code> method. | |
| 127 | * @param chr the characters to write | |
| 128 | * @throws IOException if an I/O error occurs | |
| 129 | */ | |
| 130 | @Override | |
| 131 | public void write(final char[] chr) throws IOException { | |
| 132 | try { | |
| 133 | 2 | int len = 0; |
| 134 | 2 | if (chr != null) { |
| 135 | 1 | len = chr.length; |
| 136 | } | |
| 137 | ||
| 138 | 2 | beforeWrite(len); |
| 139 | 2 | out.write(chr); |
| 140 | 2 | afterWrite(len); |
| 141 | 0 | } catch (final IOException e) { |
| 142 | 0 | handleIOException(e); |
| 143 | 2 | } |
| 144 | 2 | } |
| 145 | ||
| 146 | /** | |
| 147 | * Invokes the delegate's <code>write(char[], int, int)</code> method. | |
| 148 | * @param chr the characters to write | |
| 149 | * @param st The start offset | |
| 150 | * @param len The number of characters to write | |
| 151 | * @throws IOException if an I/O error occurs | |
| 152 | */ | |
| 153 | @Override | |
| 154 | public void write(final char[] chr, final int st, final int len) throws IOException { | |
| 155 | try { | |
| 156 | 2 | beforeWrite(len); |
| 157 | 2 | out.write(chr, st, len); |
| 158 | 2 | afterWrite(len); |
| 159 | 0 | } catch (final IOException e) { |
| 160 | 0 | handleIOException(e); |
| 161 | 2 | } |
| 162 | 2 | } |
| 163 | ||
| 164 | /** | |
| 165 | * Invokes the delegate's <code>write(String)</code> method. | |
| 166 | * @param str the string to write | |
| 167 | * @throws IOException if an I/O error occurs | |
| 168 | */ | |
| 169 | @Override | |
| 170 | public void write(final String str) throws IOException { | |
| 171 | try { | |
| 172 | 2 | int len = 0; |
| 173 | 2 | if (str != null) { |
| 174 | 1 | len = str.length(); |
| 175 | } | |
| 176 | ||
| 177 | 2 | beforeWrite(len); |
| 178 | 2 | out.write(str); |
| 179 | 2 | afterWrite(len); |
| 180 | 0 | } catch (final IOException e) { |
| 181 | 0 | handleIOException(e); |
| 182 | 2 | } |
| 183 | 2 | } |
| 184 | ||
| 185 | /** | |
| 186 | * Invokes the delegate's <code>write(String)</code> method. | |
| 187 | * @param str the string to write | |
| 188 | * @param st The start offset | |
| 189 | * @param len The number of characters to write | |
| 190 | * @throws IOException if an I/O error occurs | |
| 191 | */ | |
| 192 | @Override | |
| 193 | public void write(final String str, final int st, final int len) throws IOException { | |
| 194 | try { | |
| 195 | 2 | beforeWrite(len); |
| 196 | 2 | out.write(str, st, len); |
| 197 | 2 | afterWrite(len); |
| 198 | 0 | } catch (final IOException e) { |
| 199 | 0 | handleIOException(e); |
| 200 | 2 | } |
| 201 | 2 | } |
| 202 | ||
| 203 | /** | |
| 204 | * Invokes the delegate's <code>flush()</code> method. | |
| 205 | * @throws IOException if an I/O error occurs | |
| 206 | */ | |
| 207 | @Override | |
| 208 | public void flush() throws IOException { | |
| 209 | try { | |
| 210 | 0 | out.flush(); |
| 211 | 0 | } catch (final IOException e) { |
| 212 | 0 | handleIOException(e); |
| 213 | 0 | } |
| 214 | 0 | } |
| 215 | ||
| 216 | /** | |
| 217 | * Invokes the delegate's <code>close()</code> method. | |
| 218 | * @throws IOException if an I/O error occurs | |
| 219 | */ | |
| 220 | @Override | |
| 221 | public void close() throws IOException { | |
| 222 | try { | |
| 223 | 8 | out.close(); |
| 224 | 0 | } catch (final IOException e) { |
| 225 | 0 | handleIOException(e); |
| 226 | 8 | } |
| 227 | 8 | } |
| 228 | ||
| 229 | /** | |
| 230 | * Invoked by the write methods before the call is proxied. The number | |
| 231 | * of chars to be written (1 for the {@link #write(int)} method, buffer | |
| 232 | * length for {@link #write(char[])}, etc.) is given as an argument. | |
| 233 | * <p> | |
| 234 | * Subclasses can override this method to add common pre-processing | |
| 235 | * functionality without having to override all the write methods. | |
| 236 | * The default implementation does nothing. | |
| 237 | * | |
| 238 | * @since 2.0 | |
| 239 | * @param n number of chars to be written | |
| 240 | * @throws IOException if the pre-processing fails | |
| 241 | */ | |
| 242 | protected void beforeWrite(final int n) throws IOException { | |
| 243 | 10 | } |
| 244 | ||
| 245 | /** | |
| 246 | * Invoked by the write methods after the proxied call has returned | |
| 247 | * successfully. The number of chars written (1 for the | |
| 248 | * {@link #write(int)} method, buffer length for {@link #write(char[])}, | |
| 249 | * etc.) is given as an argument. | |
| 250 | * <p> | |
| 251 | * Subclasses can override this method to add common post-processing | |
| 252 | * functionality without having to override all the write methods. | |
| 253 | * The default implementation does nothing. | |
| 254 | * | |
| 255 | * @since 2.0 | |
| 256 | * @param n number of chars written | |
| 257 | * @throws IOException if the post-processing fails | |
| 258 | */ | |
| 259 | protected void afterWrite(final int n) throws IOException { | |
| 260 | 10 | } |
| 261 | ||
| 262 | /** | |
| 263 | * Handle any IOExceptions thrown. | |
| 264 | * <p> | |
| 265 | * This method provides a point to implement custom exception | |
| 266 | * handling. The default behaviour is to re-throw the exception. | |
| 267 | * @param e The IOException thrown | |
| 268 | * @throws IOException if an I/O error occurs | |
| 269 | * @since 2.0 | |
| 270 | */ | |
| 271 | protected void handleIOException(final IOException e) throws IOException { | |
| 272 | 0 | throw e; |
| 273 | } | |
| 274 | ||
| 275 | } |