| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.bulenkov.iconloader.util; |
| 18 |
|
| 19 |
import java.io.IOException; |
| 20 |
import java.io.OutputStream; |
| 21 |
import java.util.Arrays; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class AsyncByteArrayOutputStream extends OutputStream { |
| 27 |
protected byte[] myBuffer; |
| 28 |
protected int myCount; |
| 29 |
|
| 30 |
public AsyncByteArrayOutputStream() { |
| 31 |
this(32); |
| 32 |
} |
| 33 |
|
| 34 |
public AsyncByteArrayOutputStream(int size) { |
| 35 |
myBuffer = new byte[size]; |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public void write(int b) { |
| 40 |
int count = myCount + 1; |
| 41 |
|
| 42 |
if (count > myBuffer.length) { |
| 43 |
myBuffer = Arrays.copyOf(myBuffer, Math.max(myBuffer.length << 1, count)); |
| 44 |
} |
| 45 |
|
| 46 |
myBuffer[myCount] = (byte) b; |
| 47 |
myCount = count; |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public void write(byte b[], int off, int len) { |
| 52 |
if ((off < 0) || (off > b.length) || (len < 0) || |
| 53 |
((off + len) > b.length) || ((off + len) < 0)) { |
| 54 |
throw new IndexOutOfBoundsException(); |
| 55 |
} else if (len == 0) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
int count = myCount + len; |
| 60 |
|
| 61 |
if (count > myBuffer.length) { |
| 62 |
myBuffer = Arrays.copyOf(myBuffer, Math.max(myBuffer.length << 1, count)); |
| 63 |
} |
| 64 |
|
| 65 |
System.arraycopy(b, off, myBuffer, myCount, len); |
| 66 |
myCount = count; |
| 67 |
} |
| 68 |
|
| 69 |
public void writeTo(OutputStream out) throws IOException { |
| 70 |
out.write(myBuffer, 0, myCount); |
| 71 |
} |
| 72 |
|
| 73 |
public void reset() { |
| 74 |
myCount = 0; |
| 75 |
} |
| 76 |
|
| 77 |
public byte[] toByteArray() { |
| 78 |
return Arrays.copyOf(myBuffer, myCount); |
| 79 |
} |
| 80 |
|
| 81 |
public int size() { |
| 82 |
return myCount; |
| 83 |
} |
| 84 |
|
| 85 |
public String toString() { |
| 86 |
return new String(myBuffer, 0, myCount); |
| 87 |
} |
| 88 |
} |