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