commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Gregory <garydgreg...@gmail.com>
Subject [io] Proposal for a ProgressOutputStream
Date Tue, 25 Sep 2012 01:57:57 GMT
Hi All,

I had a need for a ProgressOutputStream that behaved in kind to:

/*
 * 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.
 */
public class ProgressOutputStreamTestCase {

    @Test
    public void testSize100in10Chunks() throws IOException {
        byte[] ba10 = new byte[10];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ProgressOutputStream pos = new ProgressOutputStream(baos, 10, new
ProgressOutputStream.Observer() {

            @Override
            public void update(ProgressOutputStream progressOutputStream) {
                System.out.print('.');
            }

            @Override
            public void flush(ProgressOutputStream progressOutputStream) {
                System.out.println();
            }
        });
        for (int i = 0; i < 10; i++) {
            pos.write(ba10);
        }
        pos.flush();
        pos.close();
        Assert.assertEquals(10, pos.getChunkCount());
    }

    @Test
    public void testSize100in10ChunksSystemOut() throws IOException {
        byte[] ba10 = new byte[10];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ProgressOutputStream pos = new ProgressOutputStream(baos, 10, new
ProgressOutputStream.SystemOutObverver());
        for (int i = 0; i < 10; i++) {
            pos.write(ba10);
        }
        pos.flush();
        pos.close();
        Assert.assertEquals(10, pos.getChunkCount());
    }
}

Which I wrote up as:

/*
 * 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.
 */

/**
 * Notifies an observer of writing progress to an OutputStream.
 */
public class ProgressOutputStream extends CountingOutputStream {

    public static interface Observer {

        void flush(ProgressOutputStream progressOutputStream);

        void update(ProgressOutputStream progressOutputStream);

    }

    public static class PrintStreamObverver implements
ProgressOutputStream.Observer {

        private final PrintStream printStream;

        public PrintStreamObverver(final PrintStream printStream) {
            this.printStream = printStream;
        }

        @Override
        public void flush(final ProgressOutputStream progressOutputStream) {
            this.printStream.println();
        }

        @Override
        public void update(final ProgressOutputStream progressOutputStream)
{
            this.printStream.print('.');
        }
    }

    public static class SystemErrObverver extends PrintStreamObverver {

        public SystemErrObverver() {
            super(System.err);
        }

    }

    public static class SystemOutObverver extends PrintStreamObverver {

        public SystemOutObverver() {
            super(System.out);
        }

    }

    private final long chunkSize;

    private long chunkCount;

    private final Observer observer;

    public ProgressOutputStream(final OutputStream proxy, final long
chunkSize, final Observer observer) {
        super(proxy);
        this.chunkSize = chunkSize;
        this.observer = observer;
    }

    @Override
    protected void afterWrite(final int n) throws IOException {
        super.afterWrite(n);
        if (this.getByteCount() % this.getChunkSize() == 0) {
            this.chunkCount++;
            this.getObserver().update(this);
        }
    }

    @Override
    public void flush() throws IOException {
        super.flush();
        this.getObserver().flush(this);
    }

    public long getChunkCount() {
        return this.chunkCount;
    }

    public long getChunkSize() {
        return this.chunkSize;
    }

    public Observer getObserver() {
        return this.observer;
    }

}

Thoughts for inclusion to [io]?

Gary
-- 
E-Mail: garydgregory@gmail.com | ggregory@apache.org
JUnit in Action, 2nd Ed: <http://goog_1249600977>http://bit.ly/ECvg0
Spring Batch in Action: <http://s.apache.org/HOq>http://bit.ly/bqpbCK
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message