Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.marmotta.platform.core.services.http;
 
 
 import java.io.*;
An implementation of org.apache.http.HttpEntity that reports write/read operations to a org.apache.marmotta.platform.core.api.task.Task.
 
 
     private MonitoredInputStream foo = null;
     private final Task           monitor;
     private final AtomicLong     bytesReceived;

    
Create a MonitoredHttpEntity based on the provided org.apache.http.HttpEntity reporting progress to the org.apache.marmotta.platform.core.api.task.Task.

Parameters:
delegate the org.apache.http.HttpEntity to wrap.
monitor the org.apache.marmotta.platform.core.api.task.Task to report the progress to.
bytesReceived java.util.concurrent.atomic.AtomicLong to add transfered bytes to.
 
     public MonitoredHttpEntity(HttpEntity delegateTask monitorAtomicLong bytesReceived) {
         super(delegate);
         this. = monitor;
         this. = bytesReceived;
 
     }

    
Create a MonitoredHttpEntity based on the provided org.apache.http.HttpEntity reporting progress to the org.apache.marmotta.platform.core.api.task.Task.

Parameters:
delegate the org.apache.http.HttpEntity to wrap.
monitor the org.apache.marmotta.platform.core.api.task.Task to report the progress to.
 
     public MonitoredHttpEntity(HttpEntity delegateTask monitor) {
         this(delegatemonitornull);
     }
 
     @Override
     public InputStream getContent() throws IOException {
         if ( == null) {
              = new MonitoredInputStream(super.getContent());
             if ( != null && getContentLength() > 0) {
                 .updateTotalSteps(getContentLength());
             }
         }
         return ;
     }
 
     @Override
     public void writeTo(OutputStream outstreamthrows IOException {
         if ( != null && getContentLength() > 0) {
             .updateTotalSteps(getContentLength());
         }
         super.writeTo(new MonitoredOutputStream(outstream));
         if ( != null) {
             .updateMessage("waiting for response");
         }
     }
 
     private void monitorHeader(final Header ct) {
         if ( != null && ct != null) {
             .updateDetailMessage(ct.getName(), ct.getValue());
         }
     }
 
     private void updateMonitor(long cPoslong delta) {
         if ( != null) {
             .updateProgress(cPos);
         }
        if ( != null) {
            .addAndGet(delta);
        }
    }
    @Override
    protected void finalize() throws Throwable {
        .endTask();
        super.finalize();
    }

    
OutputStream that reports the progress on every write operation.
    protected class MonitoredOutputStream extends FilterOutputStream {
        private long cPos;
        public MonitoredOutputStream(OutputStream out) {
            super(out);
             = 0;
        }
        @Override
        public void write(int bthrows IOException {
            super.write(b);
            updateMonitor(++, 1);
        }
        @Override
        public void write(byte[] bint offint lenthrows IOException {
            /* copied from FilterOutputStream.write(byte[], int, int) */
            if ((off | len | b.length - (len + off) | off + len) < 0)
                throw new IndexOutOfBoundsException();
            for (int i = 0; i < leni++) {
                super.write(b[off + i]);
            }
            updateMonitor( += lenlen);
        }
        @Override
        public void close() throws IOException {
            super.close();
        }
    }

    
InputStream that reports the progress on every read operation.
    protected class MonitoredInputStream extends FilterInputStream {
        private long cPosmarkPos;
        protected MonitoredInputStream(InputStream in) {
            super(in);
             = 0;
        }
        @Override
        public int read() throws IOException {
            final int i = super.read();
            if (i >= 0) {
                updateMonitor(++, 1);
            }
            return i;
        }
        @Override
        public int read(byte[] bthrows IOException {
            final int i = super.read(b);
            if (i > 0) {
                updateMonitor( += ii);
            }
            return i;
        }
        @Override
        public int read(byte[] bint offint lenthrows IOException {
            final int i = super.read(bofflen);
            if (i > 0) {
                updateMonitor( += ii);
            }
            return i;
        }
        @Override
        public long skip(long nthrows IOException {
            final long i = super.skip(n);
            if (i > 0) {
                updateMonitor( += ii);
            }
            return i;
        }
        @Override
        public synchronized void mark(int readlimit) {
            super.mark(readlimit);
             = ;
        }
        @Override
        public synchronized void reset() throws IOException {
            super.reset();
            updateMonitor( = , 0);
        }
        @Override
        public void close() throws IOException {
            .endTask();
            super.close();
        }
    }
New to GrepCode? Check out our FAQ X