mobile/android/base/background/bagheera/BoundedByteArrayEntity.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 package org.mozilla.gecko.background.bagheera;
     7 import java.io.ByteArrayInputStream;
     8 import java.io.IOException;
     9 import java.io.InputStream;
    10 import java.io.OutputStream;
    12 import ch.boye.httpclientandroidlib.entity.AbstractHttpEntity;
    13 import ch.boye.httpclientandroidlib.entity.ByteArrayEntity;
    15 /**
    16  * An entity that acts like {@link ByteArrayEntity}, but exposes a window onto
    17  * the byte array that is a subsection of the array. The purpose of this is to
    18  * allow a smaller entity to be created without having to resize the source
    19  * array.
    20  */
    21 public class BoundedByteArrayEntity extends AbstractHttpEntity implements
    22     Cloneable {
    23   protected final byte[] content;
    24   protected final int    start;
    25   protected final int    end;
    26   protected final int    length;
    28   /**
    29    * Create a new entity that behaves exactly like a {@link ByteArrayEntity}
    30    * created with a copy of <code>b</code> truncated to (
    31    * <code>end - start</code>) bytes, starting at <code>start</code>.
    32    *
    33    * @param b the byte array to use.
    34    * @param start the start index.
    35    * @param end the end index.
    36    */
    37   public BoundedByteArrayEntity(final byte[] b, final int start, final int end) {
    38     if (b == null) {
    39       throw new IllegalArgumentException("Source byte array may not be null.");
    40     }
    42     if (end < start ||
    43         start < 0   ||
    44         end   < 0   ||
    45         start > b.length ||
    46         end   > b.length) {
    47       throw new IllegalArgumentException("Bounds out of range.");
    48     }
    49     this.content = b;
    50     this.start = start;
    51     this.end = end;
    52     this.length = end - start;
    53   }
    55   @Override
    56   public boolean isRepeatable() {
    57     return true;
    58   }
    60   @Override
    61   public long getContentLength() {
    62     return this.length;
    63   }
    65   @Override
    66   public InputStream getContent() {
    67     return new ByteArrayInputStream(this.content, this.start, this.length);
    68   }
    70   @Override
    71   public void writeTo(final OutputStream outstream) throws IOException {
    72     if (outstream == null) {
    73       throw new IllegalArgumentException("Output stream may not be null.");
    74     }
    75     outstream.write(this.content);
    76     outstream.flush();
    77   }
    79   @Override
    80   public boolean isStreaming() {
    81     return false;
    82   }
    84   @Override
    85   public Object clone() throws CloneNotSupportedException {
    86     return super.clone();
    87   }
    88 }

mercurial