YW, and there would be a similar ProgressInputStream.

Gary

On Tue, Sep 25, 2012 at 11:52 AM, Felix Meschberger <fmesc...@adobe.com>wrote:

> Thanks for the hints.
>
> Regards
> Felix
>
> Am 25.09.2012 um 17:50 schrieb Gary Gregory:
>
> > On Tue, Sep 25, 2012 at 2:23 AM, Felix Meschberger <fmesc...@adobe.com
> >wrote:
> >
> >> Hi
> >>
> >> Wouldn't it also be interesting to know how much progress was made -- if
> >> at all possible ?
> >>
> >
> > Because ProgressOutputStream extends CountingOutputStream you can call
> > getByteCount() in your observer.
> >
> >
> >> For example: having a prospective number of bytes to write and have the
> >> update method called with the number of bytes just written ?
> >>
> >
> > I suppose you could have ProgressPercentageOutputStream that you can
> build
> > with an expectedByteCount and a chunkCount to get a (possibly incorrect)
> > percentage. The trick with OutputStream is that you do not know how big
> > they are... but if you are starting with a File, you can get the file
> > length...
> >
> > Gary
> >
> >>
> >> Regards
> >> Felix
> >>
> >> Am 25.09.2012 um 03:57 schrieb Gary Gregory:
> >>
> >>> 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: garydgreg...@gmail.com | ggreg...@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
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
> >> For additional commands, e-mail: dev-h...@commons.apache.org
> >>
> >>
> >
> >
> > --
> > E-Mail: garydgreg...@gmail.com | ggreg...@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
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
> For additional commands, e-mail: dev-h...@commons.apache.org
>
>


-- 
E-Mail: garydgreg...@gmail.com | ggreg...@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

Reply via email to