查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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    *   https://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.handler.codec.marshalling;
17  
18  import io.netty.buffer.ByteBuf;
19  import org.jboss.marshalling.ByteOutput;
20  
21  import java.io.IOException;
22  
23  /**
24   * {@link ByteOutput} implementation which writes the data to a {@link ByteBuf}
25   *
26   *
27   */
28  class ChannelBufferByteOutput implements ByteOutput {
29  
30      private final ByteBuf buffer;
31  
32      /**
33       * Create a new instance which use the given {@link ByteBuf}
34       */
35      ChannelBufferByteOutput(ByteBuf buffer) {
36          this.buffer = buffer;
37      }
38  
39      @Override
40      public void close() throws IOException {
41          // Nothing to do
42      }
43  
44      @Override
45      public void flush() throws IOException {
46          // nothing to do
47      }
48  
49      @Override
50      public void write(int b) throws IOException {
51          buffer.writeByte(b);
52      }
53  
54      @Override
55      public void write(byte[] bytes) throws IOException {
56          buffer.writeBytes(bytes);
57      }
58  
59      @Override
60      public void write(byte[] bytes, int srcIndex, int length) throws IOException {
61          buffer.writeBytes(bytes, srcIndex, length);
62      }
63  
64      /**
65       * Return the {@link ByteBuf} which contains the written content
66       *
67       */
68      ByteBuf getBuffer() {
69          return buffer;
70      }
71  }