1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.util;
21
22 import org.apache.hadoop.hbase.HRegionInfo;
23 import org.apache.hadoop.hbase.migration.HRegionInfo090x;
24 import org.apache.hadoop.io.DataInputBuffer;
25 import org.apache.hadoop.io.Writable;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.DataInputStream;
30 import java.io.DataOutputStream;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.List;
34
35
36
37
38 public class Writables {
39
40
41
42
43
44
45
46 public static byte [] getBytes(final Writable w) throws IOException {
47 if (w == null) {
48 throw new IllegalArgumentException("Writable cannot be null");
49 }
50 ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
51 DataOutputStream out = new DataOutputStream(byteStream);
52 try {
53 w.write(out);
54 out.close();
55 out = null;
56 return byteStream.toByteArray();
57 } finally {
58 if (out != null) {
59 out.close();
60 }
61 }
62 }
63
64
65
66
67
68
69
70
71
72 public static byte [] getBytes(final Writable... ws) throws IOException {
73 List<byte []> bytes = new ArrayList<byte []>();
74 int size = 0;
75 for (Writable w: ws) {
76 byte [] b = getBytes(w);
77 size += b.length;
78 bytes.add(b);
79 }
80 byte [] result = new byte[size];
81 int offset = 0;
82 for (byte [] b: bytes) {
83 System.arraycopy(b, 0, result, offset, b.length);
84 offset += b.length;
85 }
86 return result;
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public static Writable getWritable(final byte [] bytes, final Writable w)
102 throws IOException {
103 return getWritable(bytes, 0, bytes.length, w);
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 public static Writable getWritable(final byte [] bytes, final int offset,
121 final int length, final Writable w)
122 throws IOException {
123 if (bytes == null || length <=0) {
124 throw new IllegalArgumentException("Can't build a writable with empty " +
125 "bytes array");
126 }
127 if (w == null) {
128 throw new IllegalArgumentException("Writable cannot be null");
129 }
130 DataInputBuffer in = new DataInputBuffer();
131 try {
132 in.reset(bytes, offset, length);
133 w.readFields(in);
134 return w;
135 } finally {
136 in.close();
137 }
138 }
139
140
141
142
143
144
145 public static HRegionInfo getHRegionInfo(final byte [] bytes)
146 throws IOException {
147 return (HRegionInfo)getWritable(bytes, new HRegionInfo());
148 }
149
150
151
152
153
154
155
156 public static List<HRegionInfo> getHRegionInfos(final byte [] bytes,
157 final int offset, final int length)
158 throws IOException {
159 if (bytes == null) {
160 throw new IllegalArgumentException("Can't build a writable with empty " +
161 "bytes array");
162 }
163 DataInputBuffer in = new DataInputBuffer();
164 List<HRegionInfo> hris = new ArrayList<HRegionInfo>();
165 try {
166 in.reset(bytes, offset, length);
167 while (in.available() > 0) {
168 HRegionInfo hri = new HRegionInfo();
169 hri.readFields(in);
170 hris.add(hri);
171 }
172 } finally {
173 in.close();
174 }
175 return hris;
176 }
177
178
179
180
181
182
183
184 public static HRegionInfo getHRegionInfoOrNull(final byte [] bytes)
185 throws IOException {
186 return (bytes == null || bytes.length <= 0)?
187 null : getHRegionInfo(bytes);
188 }
189
190
191
192
193
194
195
196
197 public static Writable copyWritable(final Writable src, final Writable tgt)
198 throws IOException {
199 return copyWritable(getBytes(src), tgt);
200 }
201
202
203
204
205
206
207
208
209 public static Writable copyWritable(final byte [] bytes, final Writable tgt)
210 throws IOException {
211 DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));
212 try {
213 tgt.readFields(dis);
214 } finally {
215 dis.close();
216 }
217 return tgt;
218 }
219 }