You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2011/10/23 13:48:59 UTC

svn commit: r1187870 - in /commons/proper/compress/trunk/src/main/java/org/apache/commons/compress: archivers/dump/DumpArchiveInputStream.java utils/CountingInputStream.java utils/CountingOutputStream.java

Author: bodewig
Date: Sun Oct 23 11:48:59 2011
New Revision: 1187870

URL: http://svn.apache.org/viewvc?rev=1187870&view=rev
Log:
Add Counting(In|Out)putStreams

Added:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/CountingInputStream.java   (with props)
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/CountingOutputStream.java   (with props)
Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java?rev=1187870&r1=1187869&r2=1187870&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java Sun Oct 23 11:48:59 2011
@@ -22,7 +22,6 @@ import org.apache.commons.compress.archi
 import org.apache.commons.compress.archivers.ArchiveInputStream;
 
 import java.io.EOFException;
-import java.io.FilterInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 
@@ -34,7 +33,6 @@ import java.util.PriorityQueue;
 import java.util.Queue;
 import java.util.Stack;
 
-
 /**
  * The DumpArchiveInputStream reads a UNIX dump archive as an InputStream.
  * Methods are provided to position at each successive entry in
@@ -73,7 +71,7 @@ public class DumpArchiveInputStream exte
      * @throws ArchiveException
      */
     public DumpArchiveInputStream(InputStream is) throws ArchiveException {
-        this.raw = new TapeInputStream(new CountingStream(is));
+        this.raw = new TapeInputStream(is);
         this.hasHitEOF = false;
 
         try {
@@ -118,6 +116,17 @@ public class DumpArchiveInputStream exte
                 });
     }
 
+    @Deprecated
+    @Override
+    public int getCount() {
+        return (int) getBytesRead();
+    }
+
+    @Override
+    public long getBytesRead() {
+        return raw.getBytesRead();
+    }
+
     /**
      * Return the archive summary information.
      */
@@ -511,29 +520,4 @@ public class DumpArchiveInputStream exte
             24);
     }
 
-    private class CountingStream extends FilterInputStream {
-        private CountingStream(final InputStream in) {
-            super(in);
-        }
-        @Override
-        public int read() throws IOException {
-            int r = in.read();
-            if (r >= 0) {
-                count(1);
-            }
-            return r;
-        }
-        @Override
-        public int read(byte[] b) throws IOException {
-            return read(b, 0, b.length);
-        }
-        @Override
-        public int read(byte[] b, int off, int len) throws IOException {
-            int r = in.read(b, off, len);
-            if (r >= 0) {
-                count(r);
-            }
-            return r;
-        }
-    }
 }

Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/CountingInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/CountingInputStream.java?rev=1187870&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/CountingInputStream.java (added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/CountingInputStream.java Sun Oct 23 11:48:59 2011
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.compress.utils;
+
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * Stream that tracks the number of bytes read.
+ * @since Apache Commons Compress 1.3
+ * @ThreadSafe
+ */
+public class CountingInputStream extends FilterInputStream {
+    private final AtomicLong bytesRead = new AtomicLong();
+
+    private CountingInputStream(final InputStream in) {
+        super(in);
+    }
+
+    @Override
+    public int read() throws IOException {
+        int r = in.read();
+        if (r >= 0) {
+            count(1);
+        }
+        return r;
+    }
+    @Override
+    public int read(byte[] b) throws IOException {
+        return read(b, 0, b.length);
+    }
+    @Override
+    public int read(byte[] b, int off, int len) throws IOException {
+        int r = in.read(b, off, len);
+        if (r >= 0) {
+            count(r);
+        }
+        return r;
+    }
+    /**
+     * Increments the counter of already read bytes.
+     * Doesn't increment if the EOF has been hit (read == -1)
+     * 
+     * @param read the number of bytes read
+     */
+    protected final void count(long read) {
+        if (read != -1) {
+            bytesRead.addAndGet(read);
+        }
+    }
+    
+    /**
+     * Returns the current number of bytes read from this stream.
+     * @return the number of read bytes
+     */
+    public long getBytesRead() {
+        return bytesRead.longValue();
+    }
+}

Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/CountingInputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/CountingOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/CountingOutputStream.java?rev=1187870&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/CountingOutputStream.java (added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/CountingOutputStream.java Sun Oct 23 11:48:59 2011
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.compress.utils;
+
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * Stream that tracks the number of bytes read.
+ * @since Apache Commons Compress 1.3
+ * @ThreadSafe
+ */
+public class CountingOutputStream extends FilterOutputStream {
+    private final AtomicLong bytesWritten = new AtomicLong();
+
+    private CountingOutputStream(final OutputStream out) {
+        super(out);
+    }
+
+    @Override
+    public void write(int b) throws IOException {
+        out.write(b);
+        count(1);
+    }
+    @Override
+    public void write(byte[] b) throws IOException {
+        write(b, 0, b.length);
+    }
+    @Override
+    public void write(byte[] b, int off, int len) throws IOException {
+        out.write(b, off, len);
+        count(len);
+    }
+
+    /**
+     * Increments the counter of already written bytes.
+     * Doesn't increment if the EOF has been hit (written == -1)
+     * 
+     * @param written the number of bytes written
+     */
+    protected void count(long written) {
+        if (written != -1) {
+            bytesWritten.addAndGet(written);
+        }
+    }
+    
+    /**
+     * Returns the current number of bytes written to this stream.
+     * @return the number of written bytes
+     */
+    public long getBytesWritten() {
+        return bytesWritten.longValue();
+    }
+}

Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/utils/CountingOutputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native