1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | |
---|
19 | package org.apache.hadoop.io; |
---|
20 | |
---|
21 | import java.io.*; |
---|
22 | |
---|
23 | /** A reusable {@link OutputStream} implementation that writes to an in-memory |
---|
24 | * buffer. |
---|
25 | * |
---|
26 | * <p>This saves memory over creating a new OutputStream and |
---|
27 | * ByteArrayOutputStream each time data is written. |
---|
28 | * |
---|
29 | * <p>Typical usage is something like the following:<pre> |
---|
30 | * |
---|
31 | * OutputBuffer buffer = new OutputBuffer(); |
---|
32 | * while (... loop condition ...) { |
---|
33 | * buffer.reset(); |
---|
34 | * ... write buffer using OutputStream methods ... |
---|
35 | * byte[] data = buffer.getData(); |
---|
36 | * int dataLength = buffer.getLength(); |
---|
37 | * ... write data to its ultimate destination ... |
---|
38 | * } |
---|
39 | * </pre> |
---|
40 | * @see DataOutputBuffer |
---|
41 | * @see InputBuffer |
---|
42 | */ |
---|
43 | public class OutputBuffer extends FilterOutputStream { |
---|
44 | |
---|
45 | private static class Buffer extends ByteArrayOutputStream { |
---|
46 | public byte[] getData() { return buf; } |
---|
47 | public int getLength() { return count; } |
---|
48 | public void reset() { count = 0; } |
---|
49 | |
---|
50 | public void write(InputStream in, int len) throws IOException { |
---|
51 | int newcount = count + len; |
---|
52 | if (newcount > buf.length) { |
---|
53 | byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)]; |
---|
54 | System.arraycopy(buf, 0, newbuf, 0, count); |
---|
55 | buf = newbuf; |
---|
56 | } |
---|
57 | IOUtils.readFully(in, buf, count, len); |
---|
58 | count = newcount; |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | private Buffer buffer; |
---|
63 | |
---|
64 | /** Constructs a new empty buffer. */ |
---|
65 | public OutputBuffer() { |
---|
66 | this(new Buffer()); |
---|
67 | } |
---|
68 | |
---|
69 | private OutputBuffer(Buffer buffer) { |
---|
70 | super(buffer); |
---|
71 | this.buffer = buffer; |
---|
72 | } |
---|
73 | |
---|
74 | /** Returns the current contents of the buffer. |
---|
75 | * Data is only valid to {@link #getLength()}. |
---|
76 | */ |
---|
77 | public byte[] getData() { return buffer.getData(); } |
---|
78 | |
---|
79 | /** Returns the length of the valid data currently in the buffer. */ |
---|
80 | public int getLength() { return buffer.getLength(); } |
---|
81 | |
---|
82 | /** Resets the buffer to empty. */ |
---|
83 | public OutputBuffer reset() { |
---|
84 | buffer.reset(); |
---|
85 | return this; |
---|
86 | } |
---|
87 | |
---|
88 | /** Writes bytes from a InputStream directly into the buffer. */ |
---|
89 | public void write(InputStream in, int length) throws IOException { |
---|
90 | buffer.write(in, length); |
---|
91 | } |
---|
92 | } |
---|