This project has retired. For details please refer to its Attic page.
DynamicChannelBufferOutputStream xref
View Javadoc

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  package org.apache.giraph.utils;
19  
20  import java.io.DataOutput;
21  import java.io.IOException;
22  import java.nio.ByteOrder;
23  
24  import io.netty.buffer.Unpooled;
25  import io.netty.buffer.ByteBuf;
26  
27  /**
28   * Special output stream that can grow as needed and dumps to a
29   * DynamicChannelBuffer.
30   */
31  public class DynamicChannelBufferOutputStream implements DataOutput {
32    /** Internal dynamic channel buffer */
33    private ByteBuf buffer;
34  
35    /**
36     * Constructor
37     *
38     * @param estimatedLength Estimated length of the buffer
39     */
40    public DynamicChannelBufferOutputStream(int estimatedLength) {
41      buffer = Unpooled.unreleasableBuffer(Unpooled.buffer(estimatedLength))
42          .order(ByteOrder.LITTLE_ENDIAN);
43      // -- TODO unresolved what are benefits of using releasable?
44      // currently nit because it is just used in 1 test file
45    }
46  
47    /**
48     * Constructor with the buffer to use
49     *
50     * @param buffer Buffer to be written to (cleared before use)
51     */
52    public DynamicChannelBufferOutputStream(ByteBuf buffer) {
53      this.buffer = buffer;
54      buffer.clear();
55    }
56  
57    /**
58     * Get the dynamic channel buffer
59     *
60     * @return dynamic channel buffer (not a copy)
61     */
62    public ByteBuf getDynamicChannelBuffer() {
63      return buffer;
64    }
65  
66    @Override
67    public void write(int b) throws IOException {
68      buffer.writeByte(b);
69    }
70  
71    @Override
72    public void write(byte[] b) throws IOException {
73      buffer.writeBytes(b);
74    }
75  
76    @Override
77    public void write(byte[] b, int off, int len) throws IOException {
78      buffer.writeBytes(b, off, len);
79    }
80  
81    @Override
82    public void writeBoolean(boolean v) throws IOException {
83      buffer.writeByte(v ? 1 : 0);
84    }
85  
86    @Override
87    public void writeByte(int v) throws IOException {
88      buffer.writeByte(v);
89    }
90  
91    @Override
92    public void writeShort(int v) throws IOException {
93      buffer.writeShort(v);
94    }
95  
96    @Override
97    public void writeChar(int v) throws IOException {
98      buffer.writeChar(v);
99    }
100 
101   @Override
102   public void writeInt(int v) throws IOException {
103     buffer.writeInt(v);
104   }
105 
106   @Override
107   public void writeLong(long v) throws IOException {
108     buffer.writeLong(v);
109   }
110 
111   @Override
112   public void writeFloat(float v) throws IOException {
113     buffer.writeFloat(v);
114   }
115 
116   @Override
117   public void writeDouble(double v) throws IOException {
118     buffer.writeDouble(v);
119   }
120 
121   @Override
122   public void writeBytes(String s) throws IOException {
123     // Note that this code is mostly copied from DataOutputStream
124     int len = s.length();
125     for (int i = 0; i < len; i++) {
126       buffer.writeByte((byte) s.charAt(i));
127     }
128   }
129 
130   @Override
131   public void writeChars(String s) throws IOException {
132     // Note that this code is mostly copied from DataOutputStream
133     int len = s.length();
134     for (int i = 0; i < len; i++) {
135       int v = s.charAt(i);
136       buffer.writeByte((v >>> 8) & 0xFF);
137       buffer.writeByte((v >>> 0) & 0xFF);
138     }
139   }
140 
141   @Override
142   public void writeUTF(String s) throws IOException {
143     // Note that this code is mostly copied from DataOutputStream
144     int strlen = s.length();
145     int utflen = 0;
146     int c;
147 
148     /* use charAt instead of copying String to char array */
149     for (int i = 0; i < strlen; i++) {
150       c = s.charAt(i);
151       if ((c >= 0x0001) && (c <= 0x007F)) {
152         utflen++;
153       } else if (c > 0x07FF) {
154         utflen += 3;
155       } else {
156         utflen += 2;
157       }
158     }
159 
160     buffer.writeByte((byte) ((utflen >>> 8) & 0xFF));
161     buffer.writeByte((byte) ((utflen >>> 0) & 0xFF));
162 
163     int i = 0;
164     for (i = 0; i < strlen; i++) {
165       c = s.charAt(i);
166       if (!((c >= 0x0001) && (c <= 0x007F))) {
167         break;
168       }
169       buffer.writeByte((byte) c);
170     }
171 
172     for (; i < strlen; i++) {
173       c = s.charAt(i);
174       if ((c >= 0x0001) && (c <= 0x007F)) {
175         buffer.writeByte((byte) c);
176 
177       } else if (c > 0x07FF) {
178         buffer.writeByte((byte) (0xE0 | ((c >> 12) & 0x0F)));
179         buffer.writeByte((byte) (0x80 | ((c >>  6) & 0x3F)));
180         buffer.writeByte((byte) (0x80 | ((c >>  0) & 0x3F)));
181       } else {
182         buffer.writeByte((byte) (0xC0 | ((c >>  6) & 0x1F)));
183         buffer.writeByte((byte) (0x80 | ((c >>  0) & 0x3F)));
184       }
185     }
186   }
187 }