View Javadoc
1   /*
2    * Copyright 2021 TiKV Project Authors.
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    * 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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  
18  package org.tikv.common.codec;
19  
20  import com.google.protobuf.ByteString;
21  import java.io.ByteArrayOutputStream;
22  import java.io.DataOutput;
23  import java.io.DataOutputStream;
24  
25  // A trivial implementation supposed to be replaced
26  public class CodecDataOutput implements DataOutput {
27    protected final DataOutputStream s;
28    // TODO: Switch to ByteBuffer if possible, or a chain of ByteBuffer
29    protected final ByteArrayOutputStream byteArray;
30  
31    public CodecDataOutput() {
32      byteArray = new ByteArrayOutputStream();
33      s = new DataOutputStream(byteArray);
34    }
35  
36    public CodecDataOutput(int size) {
37      byteArray = new ByteArrayOutputStream(size);
38      s = new DataOutputStream(byteArray);
39    }
40  
41    @Override
42    public void write(int b) {
43      try {
44        s.write(b);
45      } catch (Exception e) {
46        throw new RuntimeException(e);
47      }
48    }
49  
50    @Override
51    public void write(byte[] b) {
52      try {
53        s.write(b);
54      } catch (Exception e) {
55        throw new RuntimeException(e);
56      }
57    }
58  
59    @Override
60    public void write(byte[] b, int off, int len) {
61      try {
62        s.write(b, off, len);
63      } catch (Exception e) {
64        throw new RuntimeException(e);
65      }
66    }
67  
68    @Override
69    public void writeBoolean(boolean v) {
70      try {
71        s.writeBoolean(v);
72      } catch (Exception e) {
73        throw new RuntimeException(e);
74      }
75    }
76  
77    @Override
78    public void writeByte(int v) {
79      try {
80        s.writeByte(v);
81      } catch (Exception e) {
82        throw new RuntimeException(e);
83      }
84    }
85  
86    @Override
87    public void writeShort(int v) {
88      try {
89        s.writeShort(v);
90      } catch (Exception e) {
91        throw new RuntimeException(e);
92      }
93    }
94  
95    @Override
96    public void writeChar(int v) {
97      try {
98        s.writeChar(v);
99      } catch (Exception e) {
100       throw new RuntimeException(e);
101     }
102   }
103 
104   @Override
105   public void writeInt(int v) {
106     try {
107       s.writeInt(v);
108     } catch (Exception e) {
109       throw new RuntimeException(e);
110     }
111   }
112 
113   @Override
114   public void writeLong(long v) {
115     try {
116       s.writeLong(v);
117     } catch (Exception e) {
118       throw new RuntimeException(e);
119     }
120   }
121 
122   @Override
123   public void writeFloat(float v) {
124     try {
125       s.writeFloat(v);
126     } catch (Exception e) {
127       throw new RuntimeException(e);
128     }
129   }
130 
131   @Override
132   public void writeDouble(double v) {
133     try {
134       s.writeDouble(v);
135     } catch (Exception e) {
136       throw new RuntimeException(e);
137     }
138   }
139 
140   @Override
141   public void writeBytes(String v) {
142     try {
143       s.writeBytes(v);
144     } catch (Exception e) {
145       throw new RuntimeException(e);
146     }
147   }
148 
149   @Override
150   public void writeChars(String v) {
151     try {
152       s.writeChars(v);
153     } catch (Exception e) {
154       throw new RuntimeException(e);
155     }
156   }
157 
158   @Override
159   public void writeUTF(String v) {
160     try {
161       s.writeUTF(v);
162     } catch (Exception e) {
163       throw new RuntimeException(e);
164     }
165   }
166 
167   public byte[] toBytes() {
168     return byteArray.toByteArray();
169   }
170 
171   public ByteString toByteString() {
172     return ByteString.copyFrom(byteArray.toByteArray());
173   }
174 
175   public int size() {
176     return this.byteArray.size();
177   }
178 
179   public void reset() {
180     this.byteArray.reset();
181   }
182 }