PageRenderTime 18ms CodeModel.GetById 6ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/release-0.2.0-rc0/hive/external/contrib/src/java/org/apache/hadoop/hive/contrib/util/typedbytes/TypedBytesWritable.java

#
Java | 89 lines | 52 code | 11 blank | 26 comment | 7 complexity | 8cfa182eca4ed55d79dfea71fa047e56 MD5 | raw file
 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
19package org.apache.hadoop.hive.contrib.util.typedbytes;
20
21import java.io.ByteArrayInputStream;
22import java.io.ByteArrayOutputStream;
23import java.io.DataInputStream;
24import java.io.DataOutputStream;
25import java.io.IOException;
26
27import org.apache.hadoop.io.BytesWritable;
28
29/**
30 * Writable for typed bytes.
31 */
32public class TypedBytesWritable extends BytesWritable {
33
34  /** Create a TypedBytesWritable. */
35  public TypedBytesWritable() {
36    super();
37  }
38
39  /** Create a TypedBytesWritable with a given byte array as initial value. */
40  public TypedBytesWritable(byte[] bytes) {
41    super(bytes);
42  }
43
44  /** Set the typed bytes from a given Java object. */
45  public void setValue(Object obj) {
46    try {
47      ByteArrayOutputStream baos = new ByteArrayOutputStream();
48      TypedBytesOutput tbo = TypedBytesOutput.get(new DataOutputStream(baos));
49      tbo.write(obj);
50      byte[] bytes = baos.toByteArray();
51      set(bytes, 0, bytes.length);
52    } catch (IOException e) {
53      throw new RuntimeException(e);
54    }
55  }
56
57  /** Get the typed bytes as a Java object. */
58  public Object getValue() {
59    try {
60      ByteArrayInputStream bais = new ByteArrayInputStream(get());
61      TypedBytesInput tbi = TypedBytesInput.get(new DataInputStream(bais));
62      Object obj = tbi.read();
63      return obj;
64    } catch (IOException e) {
65      throw new RuntimeException(e);
66    }
67  }
68
69  /** Get the type code embedded in the first byte. */
70  public Type getType() {
71    byte[] bytes = get();
72    if (bytes == null || bytes.length == 0) {
73      return null;
74    }
75    for (Type type : Type.values()) {
76      if (type.code == bytes[0]) {
77        return type;
78      }
79    }
80    return null;
81  }
82
83  /** Generate a suitable string representation. */
84  @Override
85  public String toString() {
86    return getValue().toString();
87  }
88
89}