订阅所有JSP/Servlet的日志 订阅 | 这是最新一篇日志 上一篇 | 下一篇日志 下一篇 ]
JAVA技术

FastByteArrayOutputStream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.io.*;
import java.util.Iterator;
import java.util.LinkedList;
 
public class FastByteArrayOutputStream extends OutputStream
{
 
    private static final int DEFAULT_BLOCK_SIZE = 8192;
    private LinkedList buffers;
    private byte buffer[];
    private boolean closed;
    private int blockSize;
    private int index;
    private int size;
 
    public FastByteArrayOutputStream()
    {
        this(8192);
    }
 
    public FastByteArrayOutputStream(int aSize)
    {
        blockSize = aSize;
        buffer = new byte[blockSize];
    }
 
    public int getSize()
    {
        return size + index;
    }
 
    public void close()
    {
        closed = true;
    }
 
    public byte[] toByteArray()
    {
        byte data[] = new byte[getSize()];
        int pos = 0;
        if(buffers != null)
        {
            for(Iterator iter = buffers.iterator(); iter.hasNext();)
            {
                byte bytes[] = (byte[])(byte[])iter.next();
                System.arraycopy(bytes, 0, data, pos, blockSize);
                pos += blockSize;
            }
 
        }
        System.arraycopy(buffer, 0, data, pos, index);
        return data;
    }
 
    public String toString()
    {
        return new String(toByteArray());
    }
 
    public void write(int datum)
        throws IOException
    {
        if(closed)
            throw new IOException("Stream closed");
        if(index == blockSize)
            addBuffer();
        buffer[index++] = (byte)datum;
    }
 
    public void write(byte data[], int offset, int length)
        throws IOException
    {
        if(data == null)
            throw new NullPointerException();
        if(offset < 0 || offset + length > data.length || length < 0)
            throw new IndexOutOfBoundsException();
        if(closed)
            throw new IOException("Stream closed");
        if(index + length > blockSize)
        {
            do
            {
                if(index == blockSize)
                    addBuffer();
                int copyLength = blockSize - index;
                if(length < copyLength)
                    copyLength = length;
                System.arraycopy(data, offset, buffer, index, copyLength);
                offset += copyLength;
                index += copyLength;
                length -= copyLength;
            } while(length > 0);
        } else
        {
            System.arraycopy(data, offset, buffer, index, length);
            index += length;
        }
    }
 
    public void writeTo(OutputStream out)
        throws IOException
    {
        if(buffers != null)
        {
            byte bytes[];
            for(Iterator iter = buffers.iterator(); iter.hasNext(); out.write(bytes, 0, blockSize))
                bytes = (byte[])(byte[])iter.next();
 
        }
        out.write(buffer, 0, index);
    }
 
    public void writeTo(RandomAccessFile out)
        throws IOException
    {
        if(buffers != null)
        {
            byte bytes[];
            for(Iterator iter = buffers.iterator(); iter.hasNext(); out.write(bytes, 0, blockSize))
                bytes = (byte[])(byte[])iter.next();
 
        }
        out.write(buffer, 0, index);
    }
 
    public void writeTo(Writer out, String encoding)
        throws IOException
    {
        if(buffers != null)
        {
            for(Iterator iter = buffers.iterator(); iter.hasNext();)
            {
                byte bytes[] = (byte[])(byte[])iter.next();
                if(encoding != null)
                    out.write(new String(bytes, encoding));
                else
                    out.write(new String(bytes));
            }
 
        }
        if(encoding != null)
            out.write(new String(buffer, 0, index, encoding));
        else
            out.write(new String(buffer, 0, index));
    }
 
    protected void addBuffer()
    {
        if(buffers == null)
            buffers = new LinkedList();
        buffers.addLast(buffer);
        buffer = new byte[blockSize];
        size += index;
        index = 0;
    }
}


平均得分
(0 次评分)





文章来自: 本站原创
标签: Fast ByteArrayOutputStream 
评论: 1 | 查看次数: 1773
  • 共有 1 条评论
  • 共有 1 条评论
发表评论
昵 称:  登录
内 容:
选 项:
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启