View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.buffer;
17  
18  import java.io.DataOutput;
19  import java.io.DataOutputStream;
20  import java.io.IOException;
21  import java.io.OutputStream;
22  
23  /**
24   * An {@link OutputStream} which writes data to a {@link ByteBuf}.
25   * <p>
26   * A write operation against this stream will occur at the {@code writerIndex}
27   * of its underlying buffer and the {@code writerIndex} will increase during
28   * the write operation.
29   * <p>
30   * This stream implements {@link DataOutput} for your convenience.
31   * The endianness of the stream is not always big endian but depends on
32   * the endianness of the underlying buffer.
33   *
34   * @see ByteBufInputStream
35   */
36  public class ByteBufOutputStream extends OutputStream implements DataOutput {
37  
38      private final ByteBuf buffer;
39      private final int startIndex;
40      private final DataOutputStream utf8out = new DataOutputStream(this);
41  
42      /**
43       * Creates a new stream which writes data to the specified {@code buffer}.
44       */
45      public ByteBufOutputStream(ByteBuf buffer) {
46          if (buffer == null) {
47              throw new NullPointerException("buffer");
48          }
49          this.buffer = buffer;
50          startIndex = buffer.writerIndex();
51      }
52  
53      /**
54       * Returns the number of written bytes by this stream so far.
55       */
56      public int writtenBytes() {
57          return buffer.writerIndex() - startIndex;
58      }
59  
60      @Override
61      public void write(byte[] b, int off, int len) throws IOException {
62          if (len == 0) {
63              return;
64          }
65  
66          buffer.writeBytes(b, off, len);
67      }
68  
69      @Override
70      public void write(byte[] b) throws IOException {
71          buffer.writeBytes(b);
72      }
73  
74      @Override
75      public void write(int b) throws IOException {
76          buffer.writeByte(b);
77      }
78  
79      @Override
80      public void writeBoolean(boolean v) throws IOException {
81          buffer.writeBoolean(v);
82      }
83  
84      @Override
85      public void writeByte(int v) throws IOException {
86          buffer.writeByte(v);
87      }
88  
89      @Override
90      public void writeBytes(String s) throws IOException {
91          ByteBufUtil.writeAscii(buffer, s);
92      }
93  
94      @Override
95      public void writeChar(int v) throws IOException {
96          buffer.writeChar(v);
97      }
98  
99      @Override
100     public void writeChars(String s) throws IOException {
101         int len = s.length();
102         for (int i = 0 ; i < len ; i ++) {
103             buffer.writeChar(s.charAt(i));
104         }
105     }
106 
107     @Override
108     public void writeDouble(double v) throws IOException {
109         buffer.writeDouble(v);
110     }
111 
112     @Override
113     public void writeFloat(float v) throws IOException {
114         buffer.writeFloat(v);
115     }
116 
117     @Override
118     public void writeInt(int v) throws IOException {
119         buffer.writeInt(v);
120     }
121 
122     @Override
123     public void writeLong(long v) throws IOException {
124         buffer.writeLong(v);
125     }
126 
127     @Override
128     public void writeShort(int v) throws IOException {
129         buffer.writeShort((short) v);
130     }
131 
132     @Override
133     public void writeUTF(String s) throws IOException {
134         utf8out.writeUTF(s);
135     }
136 
137     /**
138      * Returns the buffer where this stream is writing data.
139      */
140     public ByteBuf buffer() {
141         return buffer;
142     }
143 }