Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d7a633c164987068672f774dfeef2adf6a42f09c (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2007 compeople AG and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * 	compeople AG (Stefan Liebig) - initial API and implementation
 *  IBM Corporation - bug fixes and enhancements
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.sar;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * The DirectByteArrayOutputStream discloses its guts (internal byte buffer and
 * byte buffer length) to avoid unnecessary allocation of byte arrays usually
 * involved with toByteArray().
 */
public class DirectByteArrayOutputStream extends ByteArrayOutputStream {

	/**
	 * Creates a new direct byte array output stream. The buffer capacity is
	 * initially as defined by super class.
	 */
	public DirectByteArrayOutputStream() {
		super();
	}

	/**
	 * Creates a new byte array output stream, with a buffer capacity of the
	 * specified size, in bytes.
	 * 
	 * @param size
	 *            the initial size.
	 * @throws IllegalArgumentException
	 *             if size is negative.
	 */
	public DirectByteArrayOutputStream(int size) {
		super(size);
	}

	/**
	 * Return the actual internal byte buffer.
	 * 
	 * @return internal byte buffer
	 */
	public final byte[] getBuffer() {
		return super.buf;
	}

	/**
	 * Return the actual length of the internal byte buffer.
	 * 
	 * @return actual length of the buffer
	 */
	public final int getBufferLength() {
		return super.count;
	}

	/**
	 * Return an input stream containing all the (shared) bytes this output
	 * stream has already consumed.
	 * 
	 * @return ByteArrayInputStream
	 */
	public ByteArrayInputStream getInputStream() {
		return new ByteArrayInputStream(super.buf, 0, super.count);
	}

}

Back to the top