Debian Codegraph @

src/com/bulenkov/iconloader/util/AsyncByteArrayOutputStream.java

raw symbols SCIP
1 /*
2 * Copyright 2000-2014 JetBrains s.r.o.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * @author Konstantin Bulenkov
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 }