1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/background/bagheera/BoundedByteArrayEntity.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,88 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.background.bagheera; 1.9 + 1.10 +import java.io.ByteArrayInputStream; 1.11 +import java.io.IOException; 1.12 +import java.io.InputStream; 1.13 +import java.io.OutputStream; 1.14 + 1.15 +import ch.boye.httpclientandroidlib.entity.AbstractHttpEntity; 1.16 +import ch.boye.httpclientandroidlib.entity.ByteArrayEntity; 1.17 + 1.18 +/** 1.19 + * An entity that acts like {@link ByteArrayEntity}, but exposes a window onto 1.20 + * the byte array that is a subsection of the array. The purpose of this is to 1.21 + * allow a smaller entity to be created without having to resize the source 1.22 + * array. 1.23 + */ 1.24 +public class BoundedByteArrayEntity extends AbstractHttpEntity implements 1.25 + Cloneable { 1.26 + protected final byte[] content; 1.27 + protected final int start; 1.28 + protected final int end; 1.29 + protected final int length; 1.30 + 1.31 + /** 1.32 + * Create a new entity that behaves exactly like a {@link ByteArrayEntity} 1.33 + * created with a copy of <code>b</code> truncated to ( 1.34 + * <code>end - start</code>) bytes, starting at <code>start</code>. 1.35 + * 1.36 + * @param b the byte array to use. 1.37 + * @param start the start index. 1.38 + * @param end the end index. 1.39 + */ 1.40 + public BoundedByteArrayEntity(final byte[] b, final int start, final int end) { 1.41 + if (b == null) { 1.42 + throw new IllegalArgumentException("Source byte array may not be null."); 1.43 + } 1.44 + 1.45 + if (end < start || 1.46 + start < 0 || 1.47 + end < 0 || 1.48 + start > b.length || 1.49 + end > b.length) { 1.50 + throw new IllegalArgumentException("Bounds out of range."); 1.51 + } 1.52 + this.content = b; 1.53 + this.start = start; 1.54 + this.end = end; 1.55 + this.length = end - start; 1.56 + } 1.57 + 1.58 + @Override 1.59 + public boolean isRepeatable() { 1.60 + return true; 1.61 + } 1.62 + 1.63 + @Override 1.64 + public long getContentLength() { 1.65 + return this.length; 1.66 + } 1.67 + 1.68 + @Override 1.69 + public InputStream getContent() { 1.70 + return new ByteArrayInputStream(this.content, this.start, this.length); 1.71 + } 1.72 + 1.73 + @Override 1.74 + public void writeTo(final OutputStream outstream) throws IOException { 1.75 + if (outstream == null) { 1.76 + throw new IllegalArgumentException("Output stream may not be null."); 1.77 + } 1.78 + outstream.write(this.content); 1.79 + outstream.flush(); 1.80 + } 1.81 + 1.82 + @Override 1.83 + public boolean isStreaming() { 1.84 + return false; 1.85 + } 1.86 + 1.87 + @Override 1.88 + public Object clone() throws CloneNotSupportedException { 1.89 + return super.clone(); 1.90 + } 1.91 +} 1.92 \ No newline at end of file