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