1 /**
2 * Copyright 2010 The Apache Software Foundation
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 package org.apache.hadoop.hbase.util;
21
22 import org.apache.hadoop.hbase.HRegionInfo;
23 import org.apache.hadoop.io.DataInputBuffer;
24 import org.apache.hadoop.io.Writable;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.DataInputStream;
29 import java.io.DataOutputStream;
30 import java.io.IOException;
31
32 /**
33 * Utility class with methods for manipulating Writable objects
34 */
35 public class Writables {
36 /**
37 * @param w writable
38 * @return The bytes of <code>w</code> gotten by running its
39 * {@link Writable#write(java.io.DataOutput)} method.
40 * @throws IOException e
41 * @see #getWritable(byte[], Writable)
42 */
43 public static byte [] getBytes(final Writable w) throws IOException {
44 if (w == null) {
45 throw new IllegalArgumentException("Writable cannot be null");
46 }
47 ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
48 DataOutputStream out = new DataOutputStream(byteStream);
49 try {
50 w.write(out);
51 out.close();
52 out = null;
53 return byteStream.toByteArray();
54 } finally {
55 if (out != null) {
56 out.close();
57 }
58 }
59 }
60
61 /**
62 * Set bytes into the passed Writable by calling its
63 * {@link Writable#readFields(java.io.DataInput)}.
64 * @param bytes serialized bytes
65 * @param w An empty Writable (usually made by calling the null-arg
66 * constructor).
67 * @return The passed Writable after its readFields has been called fed
68 * by the passed <code>bytes</code> array or IllegalArgumentException
69 * if passed null or an empty <code>bytes</code> array.
70 * @throws IOException e
71 * @throws IllegalArgumentException
72 */
73 public static Writable getWritable(final byte [] bytes, final Writable w)
74 throws IOException {
75 return getWritable(bytes, 0, bytes.length, w);
76 }
77
78 /**
79 * Set bytes into the passed Writable by calling its
80 * {@link Writable#readFields(java.io.DataInput)}.
81 * @param bytes serialized bytes
82 * @param offset offset into array
83 * @param length length of data
84 * @param w An empty Writable (usually made by calling the null-arg
85 * constructor).
86 * @return The passed Writable after its readFields has been called fed
87 * by the passed <code>bytes</code> array or IllegalArgumentException
88 * if passed null or an empty <code>bytes</code> array.
89 * @throws IOException e
90 * @throws IllegalArgumentException
91 */
92 public static Writable getWritable(final byte [] bytes, final int offset,
93 final int length, final Writable w)
94 throws IOException {
95 if (bytes == null || length <=0) {
96 throw new IllegalArgumentException("Can't build a writable with empty " +
97 "bytes array");
98 }
99 if (w == null) {
100 throw new IllegalArgumentException("Writable cannot be null");
101 }
102 DataInputBuffer in = new DataInputBuffer();
103 try {
104 in.reset(bytes, offset, length);
105 w.readFields(in);
106 return w;
107 } finally {
108 in.close();
109 }
110 }
111
112 /**
113 * @param bytes serialized bytes
114 * @return A HRegionInfo instance built out of passed <code>bytes</code>.
115 * @throws IOException e
116 */
117 public static HRegionInfo getHRegionInfo(final byte [] bytes)
118 throws IOException {
119 return (HRegionInfo)getWritable(bytes, new HRegionInfo());
120 }
121
122 /**
123 * @param bytes serialized bytes
124 * @return A HRegionInfo instance built out of passed <code>bytes</code>
125 * or <code>null</code> if passed bytes are null or an empty array.
126 * @throws IOException e
127 */
128 public static HRegionInfo getHRegionInfoOrNull(final byte [] bytes)
129 throws IOException {
130 return (bytes == null || bytes.length <= 0)?
131 null : getHRegionInfo(bytes);
132 }
133
134 /**
135 * Copy one Writable to another. Copies bytes using data streams.
136 * @param src Source Writable
137 * @param tgt Target Writable
138 * @return The target Writable.
139 * @throws IOException e
140 */
141 public static Writable copyWritable(final Writable src, final Writable tgt)
142 throws IOException {
143 return copyWritable(getBytes(src), tgt);
144 }
145
146 /**
147 * Copy one Writable to another. Copies bytes using data streams.
148 * @param bytes Source Writable
149 * @param tgt Target Writable
150 * @return The target Writable.
151 * @throws IOException e
152 */
153 public static Writable copyWritable(final byte [] bytes, final Writable tgt)
154 throws IOException {
155 DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));
156 try {
157 tgt.readFields(dis);
158 } finally {
159 dis.close();
160 }
161 return tgt;
162 }
163 }