1 | /** |
2 | * Copyright 2008 ATG DUST Project |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * |
7 | * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
8 | * |
9 | * Unless required by applicable law or agreed to in writing, software |
10 | * distributed under the License is distributed on an "AS IS" BASIS, |
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | * See the License for the specific language governing permissions and limitations under the License. |
13 | */ |
14 | package atg.test.io; |
15 | |
16 | import java.io.IOException; |
17 | import java.io.OutputStream; |
18 | import java.nio.ByteBuffer; |
19 | |
20 | /** |
21 | * An output stream which allows writing to a ByteBuffer |
22 | * |
23 | * @author Adam Belmont |
24 | * @version $Id:$ |
25 | * @see ByteBuffer |
26 | */ |
27 | public class ByteBufferOutputStream extends OutputStream { |
28 | private ByteBuffer mBuffer = null; |
29 | |
30 | /** |
31 | * Creates a new ByteBufferOutputStream which writes into the given |
32 | * ByteBuffer. Note that you should class ByteBuffer.flip() after writing to |
33 | * this stream in order to make the buffer available for reading with a |
34 | * ByteBufferInputStream. |
35 | * |
36 | * @param pBuffer |
37 | * @see ByteBufferInputStream |
38 | */ |
39 | public ByteBufferOutputStream(ByteBuffer pBuffer) { |
40 | mBuffer = pBuffer; |
41 | } |
42 | |
43 | @Override |
44 | public synchronized void write(int b) throws IOException { |
45 | mBuffer.put((byte) b); |
46 | } |
47 | |
48 | @Override |
49 | public synchronized void write(byte[] bytes, int off, int len) |
50 | throws IOException { |
51 | mBuffer.put(bytes, off, len); |
52 | } |
53 | } |