File: ByteArrayOStream.java

package info (click to toggle)
emma-coverage 2.0.5312%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 2,000 kB
  • ctags: 3,667
  • sloc: java: 23,109; xml: 414; makefile: 22
file content (320 lines) | stat: -rw-r--r-- 9,780 bytes parent folder | download | duplicates (3)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
 * 
 * This program and the accompanying materials are made available under
 * the terms of the Common Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v10.html
 * 
 * $Id: ByteArrayOStream.java,v 1.1.1.1 2004/05/09 16:57:52 vlad_r Exp $
 */
package com.vladium.util;

import java.io.IOException;
import java.io.OutputStream;

import com.vladium.util.asserts.$assert;

// ----------------------------------------------------------------------------
/**
 * An unsynchronized version of java.io.ByteArrayOutputStream that can expose
 * the underlying byte array without a defensive clone and can also be converted
 * to a {@link ByteArrayIStream} without intermediate array copies.<p> 
 * 
 * All argument validation is disabled in release mode.<p>
 * 
 * NOTE: copy-on-write not supported
 * 
 * @author (C) 2001, Vlad Roubtsov
 */
public
final class ByteArrayOStream extends OutputStream
{
    // public: ................................................................
    
    /**
     * Callee takes ownership of 'buf'.
     */
    public ByteArrayOStream (final int initialCapacity)
    {
        if ($assert.ENABLED)
            $assert.ASSERT (initialCapacity >= 0, "negative initial capacity: " + initialCapacity); 
        
        m_buf = new byte [initialCapacity];
    }
    
    public final ByteArrayIStream toByteIStream ()
    {
        return new ByteArrayIStream (m_buf, m_pos);
    }
    
    public final void write2 (final int b1, final int b2)
    {
        final int pos = m_pos;
        final int capacity = pos + 2;
        byte [] mbuf = m_buf;
        final int mbuflen = mbuf.length;
        
        if (mbuflen < capacity)
        {
            final byte [] newbuf = new byte [Math.max (mbuflen << 1, capacity)];
        
            if (pos < NATIVE_COPY_THRESHOLD)
                for (int i = 0; i < pos; ++ i) newbuf [i] = mbuf [i];
            else
                System.arraycopy (mbuf, 0, newbuf, 0, pos);
            
            m_buf = mbuf = newbuf;
        }
        
        mbuf [pos] = (byte) b1;
        mbuf [pos + 1] = (byte) b2;
        m_pos = capacity;
    }
    
    public final void write3 (final int b1, final int b2, final int b3)
    {
        final int pos = m_pos;
        final int capacity = pos + 3;
        byte [] mbuf = m_buf;
        final int mbuflen = mbuf.length;
        
        if (mbuflen < capacity)
        {
            final byte [] newbuf = new byte [Math.max (mbuflen << 1, capacity)];
        
            if (pos < NATIVE_COPY_THRESHOLD)
                for (int i = 0; i < pos; ++ i) newbuf [i] = mbuf [i];
            else
                System.arraycopy (mbuf, 0, newbuf, 0, pos);
            
            m_buf = mbuf = newbuf;
        }
        
        mbuf [pos] = (byte) b1;
        mbuf [pos + 1] = (byte) b2;
        mbuf [pos + 2] = (byte) b3;
        m_pos = capacity;
    }
    
    public final void write4 (final int b1, final int b2, final int b3, final int b4)
    {
        final int pos = m_pos;
        final int capacity = pos + 4;
        byte [] mbuf = m_buf;
        final int mbuflen = mbuf.length;
        
        if (mbuflen < capacity)
        {
            final byte [] newbuf = new byte [Math.max (mbuflen << 1, capacity)];
        
            if (pos < NATIVE_COPY_THRESHOLD)
                for (int i = 0; i < pos; ++ i) newbuf [i] = mbuf [i];
            else
                System.arraycopy (mbuf, 0, newbuf, 0, pos);
            
            m_buf = mbuf = newbuf;
        }
        
        mbuf [pos] = (byte) b1;
        mbuf [pos + 1] = (byte) b2;
        mbuf [pos + 2] = (byte) b3;
        mbuf [pos + 3] = (byte) b4;
        m_pos = capacity;
    }
    
    public final void writeTo (final OutputStream out)
        throws IOException
    {
        out.write (m_buf, 0, m_pos);
    }
    
//    public final void readFully (final InputStream in)
//        throws IOException
//    {
//        while (true)
//        {
//            int chunk = in.available ();
//            
//            System.out.println ("available = " + chunk);
//            
//            // TODO: this case is handled poorly (on EOF)
//            if (chunk == 0) chunk = READ_CHUNK_SIZE;
//            
//            // read at least 'available' bytes: extend the capacity as needed
//        
//            int free = m_buf.length - m_pos;
//            
//            final int read;
//            if (free > chunk)
//            {
//                // try reading more than 'chunk' anyway:
//                read = in.read (m_buf, m_pos, free);
//            }
//            else
//            {
//                // extend the capacity to match 'chunk':
//                {
//                    System.out.println ("reallocation");
//                    final byte [] newbuf = new byte [m_pos + chunk];
//            
//                    if (m_pos < NATIVE_COPY_THRESHOLD)
//                        for (int i = 0; i < m_pos; ++ i) newbuf [i] = m_buf [i];
//                    else
//                        System.arraycopy (m_buf, 0, newbuf, 0, m_pos);
//                
//                    m_buf = newbuf;
//                }
//                
//                read = in.read (m_buf, m_pos, chunk);
//            }
//
//            if (read < 0)
//                break;
//            else
//                m_pos += read;
//        }
//    }
    
//    public final void addCapacity (final int extraCapacity)
//    {
//        final int pos = m_pos;
//        final int capacity = pos + extraCapacity;
//        byte [] mbuf = m_buf;
//        final int mbuflen = mbuf.length;
//        
//        if (mbuflen < capacity)
//        {
//            final byte [] newbuf = new byte [Math.max (mbuflen << 1, capacity)];
//        
//            if (pos < NATIVE_COPY_THRESHOLD)
//                for (int i = 0; i < pos; ++ i) newbuf [i] = mbuf [i];
//            else
//                System.arraycopy (mbuf, 0, newbuf, 0, pos);
//            
//            m_buf = newbuf;
//        }
//    }
    
    public final byte [] getByteArray ()
    {
        return m_buf;
    }

    /**
     * 
     * @return [result.length = size()]
     */    
    public final byte [] copyByteArray ()
    {
        final int pos = m_pos;
        
        final byte [] result = new byte [pos];
        final byte [] mbuf = m_buf;
        
        if (pos < NATIVE_COPY_THRESHOLD)
            for (int i = 0; i < pos; ++ i) result [i] = mbuf [i];
        else
            System.arraycopy (mbuf, 0, result, 0, pos);
        
        return result;
    }
    
    public final int size ()
    {
        return m_pos;
    }
    
    public final int capacity ()
    {
        return m_buf.length;
    }
    
    /**
     * Does not reduce the current capacity.
     */
    public final void reset ()
    {
        m_pos = 0;
    }
    
    // OutputStream:
    
    public final void write (final int b)
    {
        final int pos = m_pos;
        final int capacity = pos + 1;
        byte [] mbuf = m_buf;
        final int mbuflen = mbuf.length;
        
        if (mbuflen < capacity)
        {
            final byte [] newbuf = new byte [Math.max (mbuflen << 1, capacity)];
            
            if (pos < NATIVE_COPY_THRESHOLD)
                for (int i = 0; i < pos; ++ i) newbuf [i] = mbuf [i];
            else
                System.arraycopy (mbuf, 0, newbuf, 0, pos);
            
            m_buf = mbuf = newbuf;
        }
        
        mbuf [pos] = (byte) b;
        m_pos = capacity;
    }


    public final void write (final byte [] buf, final int offset, final int length)
    {
        if ($assert.ENABLED)
            $assert.ASSERT ((offset >= 0) && (offset <= buf.length) &&
                (length >= 0) && ((offset + length) <= buf.length),
                "invalid input (" + buf.length + ", " + offset + ", " + length + ")");
        
        final int pos = m_pos;
        final int capacity = pos + length;
        byte [] mbuf = m_buf;
        final int mbuflen = mbuf.length;
        
        if (mbuflen < capacity)
        {
            final byte [] newbuf = new byte [Math.max (mbuflen << 1, capacity)];
            
            if (pos < NATIVE_COPY_THRESHOLD)
                for (int i = 0; i < pos; ++ i) newbuf [i] = mbuf [i];
            else
                System.arraycopy (mbuf, 0, newbuf, 0, pos);
            
            m_buf = mbuf = newbuf;
        }
        
        if (length < NATIVE_COPY_THRESHOLD)
            for (int i = 0; i < length; ++ i) mbuf [pos + i] = buf [offset + i];
        else
            System.arraycopy (buf, offset, mbuf, pos, length);
            
        m_pos = capacity; 
    }

    
    /**
     * Equivalent to {@link #reset()}. 
     */
    public final void close ()
    {
        reset ();
    }
    
    // protected: .............................................................

    // package: ...............................................................
    
    // private: ...............................................................
    
    
    private byte [] m_buf;
    private int m_pos;
    
//    private static final int READ_CHUNK_SIZE        = 16 * 1024;
    private static final int NATIVE_COPY_THRESHOLD  = 9;

} // end of class
// ----------------------------------------------------------------------------