01: /*
02: * BEGIN_HEADER - DO NOT EDIT
03: *
04: * The contents of this file are subject to the terms
05: * of the Common Development and Distribution License
06: * (the "License"). You may not use this file except
07: * in compliance with the License.
08: *
09: * You can obtain a copy of the license at
10: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
11: * See the License for the specific language governing
12: * permissions and limitations under the License.
13: *
14: * When distributing Covered Code, include this CDDL
15: * HEADER in each file and include the License file at
16: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
17: * If applicable add the following below this CDDL HEADER,
18: * with the fields enclosed by brackets "[]" replaced with
19: * your own identifying information: Portions Copyright
20: * [year] [name of copyright owner]
21: */
22:
23: /*
24: * @(#)OpenBufferOutputStream.java
25: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
26: *
27: * END_HEADER - DO NOT EDIT
28: */
29: package com.sun.jbi.messaging.util;
30:
31: import java.io.InputStream;
32: import java.io.ByteArrayInputStream;
33:
34: /** Special version of <code>ByteArrayOutputStream</code> which allows direct
35: * access to the underlying byte buffer. If left on it's own, BAOS will copy
36: * the entire buffer every time you access it. Basically, we lose a bit a
37: * safety in order to gain a ton of performance. So be careful!
38: * @author Sun Microsystems, Inc.
39: */
40: public class OpenBufferOutputStream extends
41: java.io.ByteArrayOutputStream {
42: /** Initializes a new buffer stream. */
43: public OpenBufferOutputStream() {
44: super ();
45: }
46:
47: /** Initializes a new buffer stream, with the specified buffer size.
48: * @param size initial size of the buffer.
49: */
50: public OpenBufferOutputStream(int size) {
51: super (size);
52: }
53:
54: /** Retrieves the raw byte buffer underneath the covers.
55: * @return raw bytes underlying the buffer.
56: */
57: public byte[] getBuffer() {
58: return buf;
59: }
60:
61: /** Returns the valid number of bytes written to the buffer.
62: * @return number of valid bytes in the buffer.
63: */
64: public int getCount() {
65: return count;
66: }
67:
68: /** Utility method used to create an appropriately sized stream based off
69: * of the underlying buffer. The stream is clipped to size, so it will not
70: * contain any garbage bytes from the end of the buffer.
71: * @return input stream pointing to bytes in the buffer
72: */
73: public InputStream asInputStream() {
74: return new ByteArrayInputStream(buf, 0, count);
75: }
76: }
|