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((byte) b);
77      }
78  
79      @Override
80      public void writeBoolean(boolean v) throws IOException {
81          write(v? (byte) 1 : (byte) 0);
82      }
83  
84      @Override
85      public void writeByte(int v) throws IOException {
86          write(v);
87      }
88  
89      @Override
90      public void writeBytes(String s) throws IOException {
91          int len = s.length();
92          for (int i = 0; i < len; i ++) {
93              write((byte) s.charAt(i));
94          }
95      }
96  
97      @Override
98      public void writeChar(int v) throws IOException {
99          writeShort((short) v);
100     }
101 
102     @Override
103     public void writeChars(String s) throws IOException {
104         int len = s.length();
105         for (int i = 0 ; i < len ; i ++) {
106             writeChar(s.charAt(i));
107         }
108     }
109 
110     @Override
111     public void writeDouble(double v) throws IOException {
112         writeLong(Double.doubleToLongBits(v));
113     }
114 
115     @Override
116     public void writeFloat(float v) throws IOException {
117         writeInt(Float.floatToIntBits(v));
118     }
119 
120     @Override
121     public void writeInt(int v) throws IOException {
122         buffer.writeInt(v);
123     }
124 
125     @Override
126     public void writeLong(long v) throws IOException {
127         buffer.writeLong(v);
128     }
129 
130     @Override
131     public void writeShort(int v) throws IOException {
132         buffer.writeShort((short) v);
133     }
134 
135     @Override
136     public void writeUTF(String s) throws IOException {
137         utf8out.writeUTF(s);
138     }
139 
140     /**
141      * Returns the buffer where this stream is writing data.
142      */
143     public ByteBuf buffer() {
144         return buffer;
145     }
146 }