commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r933964 - in /commons/proper/io/trunk/src/java/org/apache/commons/io: input/ProxyReader.java output/ProxyWriter.java
Date Wed, 14 Apr 2010 14:15:08 GMT
Author: jukka
Date: Wed Apr 14 14:15:08 2010
New Revision: 933964

URL: http://svn.apache.org/viewvc?rev=933964&view=rev
Log:
IO-242: Pre- and post-processing support for ProxyReader/Writer

Modified:
    commons/proper/io/trunk/src/java/org/apache/commons/io/input/ProxyReader.java
    commons/proper/io/trunk/src/java/org/apache/commons/io/output/ProxyWriter.java

Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/input/ProxyReader.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/input/ProxyReader.java?rev=933964&r1=933963&r2=933964&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/input/ProxyReader.java (original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/input/ProxyReader.java Wed Apr
14 14:15:08 2010
@@ -53,7 +53,10 @@ public abstract class ProxyReader extend
     @Override
     public int read() throws IOException {
         try {
-            return in.read();
+            beforeRead(1);
+            int c = in.read();
+            afterRead(c != -1 ? 1 : -1);
+            return c;
         } catch (IOException e) {
             handleIOException(e);
             return -1;
@@ -69,7 +72,10 @@ public abstract class ProxyReader extend
     @Override
     public int read(char[] chr) throws IOException {
         try {
-            return in.read(chr);
+            beforeRead(chr.length);
+            int n = in.read(chr);
+            afterRead(n);
+            return n;
         } catch (IOException e) {
             handleIOException(e);
             return -1;
@@ -80,14 +86,17 @@ public abstract class ProxyReader extend
      * Invokes the delegate's <code>read(char[], int, int)</code> method.
      * @param chr the buffer to read the characters into
      * @param st The start offset
-     * @param end The number of bytes to read
+     * @param len The number of bytes to read
      * @return the number of characters read or -1 if the end of stream
      * @throws IOException if an I/O error occurs
      */
     @Override
-    public int read(char[] chr, int st, int end) throws IOException {
+    public int read(char[] chr, int st, int len) throws IOException {
         try {
-            return in.read(chr, st, end);
+            beforeRead(len);
+            int n = in.read(chr, st, len);
+            afterRead(n);
+            return n;
         } catch (IOException e) {
             handleIOException(e);
             return -1;
@@ -104,7 +113,10 @@ public abstract class ProxyReader extend
     @Override
     public int read(CharBuffer target) throws IOException {
         try {
-            return in.read(target);
+            beforeRead(target.length());
+            int n = in.read(target);
+            afterRead(n);
+            return n;
         } catch (IOException e) {
             handleIOException(e);
             return -1;
@@ -192,6 +204,47 @@ public abstract class ProxyReader extend
     }
 
     /**
+     * Invoked by the read methods before the call is proxied. The number
+     * of chars that the caller wanted to read (1 for the {@link #read()}
+     * method, buffer length for {@link #read(char[])}, etc.) is given as
+     * an argument.
+     * <p>
+     * Subclasses can override this method to add common pre-processing
+     * functionality without having to override all the read methods.
+     * The default implementation does nothing.
+     * <p>
+     * Note this method is <em>not</em> called from {@link #skip(long)} or
+     * {@link #reset()}. You need to explicitly override those methods if
+     * you want to add pre-processing steps also to them.
+     *
+     * @since Commons IO 2.0
+     * @param n number of chars that the caller asked to be read
+     * @throws IOException if the pre-processing fails
+     */
+    protected void beforeRead(int n) throws IOException {
+    }
+
+    /**
+     * Invoked by the read methods after the proxied call has returned
+     * successfully. The number of chars returned to the caller (or -1 if
+     * the end of stream was reached) is given as an argument.
+     * <p>
+     * Subclasses can override this method to add common post-processing
+     * functionality without having to override all the read methods.
+     * The default implementation does nothing.
+     * <p>
+     * Note this method is <em>not</em> called from {@link #skip(long)} or
+     * {@link #reset()}. You need to explicitly override those methods if
+     * you want to add post-processing steps also to them.
+     *
+     * @since Commons IO 2.0
+     * @param n number of chars read, or -1 if the end of stream was reached
+     * @throws IOException if the post-processing fails
+     */
+    protected void afterRead(int n) throws IOException {
+    }
+
+    /**
      * Handle any IOExceptions thrown.
      * <p>
      * This method provides a point to implement custom exception

Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/output/ProxyWriter.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/output/ProxyWriter.java?rev=933964&r1=933963&r2=933964&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/output/ProxyWriter.java (original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/output/ProxyWriter.java Wed Apr
14 14:15:08 2010
@@ -53,7 +53,9 @@ public class ProxyWriter extends FilterW
     @Override
     public Writer append(char c) throws IOException {
         try {
+            beforeWrite(1);
             out.append(c);
+            afterWrite(1);
         } catch (IOException e) {
             handleIOException(e);
         }
@@ -72,7 +74,9 @@ public class ProxyWriter extends FilterW
     @Override
     public Writer append(CharSequence csq, int start, int end) throws IOException {
         try {
+            beforeWrite(end - start);
             out.append(csq, start, end);
+            afterWrite(end - start);
         } catch (IOException e) {
             handleIOException(e);
         }
@@ -89,7 +93,9 @@ public class ProxyWriter extends FilterW
     @Override
     public Writer append(CharSequence csq) throws IOException {
         try {
+            beforeWrite(csq.length());
             out.append(csq);
+            afterWrite(csq.length());
         } catch (IOException e) {
             handleIOException(e);
         }
@@ -104,7 +110,9 @@ public class ProxyWriter extends FilterW
     @Override
     public void write(int idx) throws IOException {
         try {
+            beforeWrite(1);
             out.write(idx);
+            afterWrite(1);
         } catch (IOException e) {
             handleIOException(e);
         }
@@ -118,7 +126,9 @@ public class ProxyWriter extends FilterW
     @Override
     public void write(char[] chr) throws IOException {
         try {
+            beforeWrite(chr.length);
             out.write(chr);
+            afterWrite(chr.length);
         } catch (IOException e) {
             handleIOException(e);
         }
@@ -128,13 +138,15 @@ public class ProxyWriter extends FilterW
      * Invokes the delegate's <code>write(char[], int, int)</code> method.
      * @param chr the characters to write
      * @param st The start offset
-     * @param end The number of characters to write
+     * @param len The number of characters to write
      * @throws IOException if an I/O error occurs
      */
     @Override
-    public void write(char[] chr, int st, int end) throws IOException {
+    public void write(char[] chr, int st, int len) throws IOException {
         try {
-            out.write(chr, st, end);
+            beforeWrite(len);
+            out.write(chr, st, len);
+            afterWrite(len);
         } catch (IOException e) {
             handleIOException(e);
         }
@@ -148,7 +160,9 @@ public class ProxyWriter extends FilterW
     @Override
     public void write(String str) throws IOException {
         try {
+            beforeWrite(str.length());
             out.write(str);
+            afterWrite(str.length());
         } catch (IOException e) {
             handleIOException(e);
         }
@@ -158,13 +172,15 @@ public class ProxyWriter extends FilterW
      * Invokes the delegate's <code>write(String)</code> method.
      * @param str the string to write
      * @param st The start offset
-     * @param end The number of characters to write
+     * @param len The number of characters to write
      * @throws IOException if an I/O error occurs
      */
     @Override
-    public void write(String str, int st, int end) throws IOException {
+    public void write(String str, int st, int len) throws IOException {
         try {
-            out.write(str, st, end);
+            beforeWrite(len);
+            out.write(str, st, len);
+            afterWrite(len);
         } catch (IOException e) {
             handleIOException(e);
         }
@@ -197,6 +213,39 @@ public class ProxyWriter extends FilterW
     }
 
     /**
+     * Invoked by the write methods before the call is proxied. The number
+     * of chars to be written (1 for the {@link #write(int)} method, buffer
+     * length for {@link #write(char[])}, etc.) is given as an argument.
+     * <p>
+     * Subclasses can override this method to add common pre-processing
+     * functionality without having to override all the write methods.
+     * The default implementation does nothing.
+     *
+     * @since Commons IO 2.0
+     * @param n number of chars to be written
+     * @throws IOException if the pre-processing fails
+     */
+    protected void beforeWrite(int n) throws IOException {
+    }
+
+    /**
+     * Invoked by the write methods after the proxied call has returned
+     * successfully. The number of chars written (1 for the
+     * {@link #write(int)} method, buffer length for {@link #write(char[])},
+     * etc.) is given as an argument.
+     * <p>
+     * Subclasses can override this method to add common post-processing
+     * functionality without having to override all the write methods.
+     * The default implementation does nothing.
+     *
+     * @since Commons IO 2.0
+     * @param n number of chars written
+     * @throws IOException if the post-processing fails
+     */
+    protected void afterWrite(int n) throws IOException {
+    }
+
+    /**
      * Handle any IOExceptions thrown.
      * <p>
      * This method provides a point to implement custom exception



Mime
View raw message