PageRenderTime 64ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/serde/src/java/org/apache/hadoop/hive/serde2/lazy/LazyByte.java

#
Java | 101 lines | 34 code | 8 blank | 59 comment | 2 complexity | 0d50be45a23d6456386275da1cfaa64e MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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.serde2.lazy;
  19. import org.apache.hadoop.hive.serde2.io.ByteWritable;
  20. import org.apache.hadoop.hive.serde2.lazy.objectinspector.primitive.LazyByteObjectInspector;
  21. /**
  22. * LazyObject for storing a value of Byte.
  23. *
  24. * <p>
  25. * Part of the code is adapted from Apache Harmony Project.
  26. *
  27. * As with the specification, this implementation relied on code laid out in <a
  28. * href="http://www.hackersdelight.org/">Henry S. Warren, Jr.'s Hacker's
  29. * Delight, (Addison Wesley, 2002)</a> as well as <a
  30. * href="http://aggregate.org/MAGIC/">The Aggregate's Magic Algorithms</a>.
  31. * </p>
  32. *
  33. */
  34. public class LazyByte extends
  35. LazyPrimitive<LazyByteObjectInspector, ByteWritable> {
  36. public LazyByte(LazyByteObjectInspector oi) {
  37. super(oi);
  38. data = new ByteWritable();
  39. }
  40. public LazyByte(LazyByte copy) {
  41. super(copy);
  42. data = new ByteWritable(copy.data.get());
  43. }
  44. @Override
  45. public void init(ByteArrayRef bytes, int start, int length) {
  46. try {
  47. data.set(parseByte(bytes.getData(), start, length, 10));
  48. isNull = false;
  49. } catch (NumberFormatException e) {
  50. isNull = true;
  51. }
  52. }
  53. /**
  54. * Parses the string argument as if it was a byte value and returns the
  55. * result. Throws NumberFormatException if the string does not represent a
  56. * single byte quantity.
  57. *
  58. * @param bytes
  59. * @param start
  60. * @param length
  61. * a UTF-8 encoded string representation of a single byte quantity.
  62. * @return byte the value represented by the argument
  63. * @throws NumberFormatException
  64. * if the argument could not be parsed as a byte quantity.
  65. */
  66. public static byte parseByte(byte[] bytes, int start, int length) {
  67. return parseByte(bytes, start, length, 10);
  68. }
  69. /**
  70. * Parses the string argument as if it was a byte value and returns the
  71. * result. Throws NumberFormatException if the string does not represent a
  72. * single byte quantity. The second argument specifies the radix to use when
  73. * parsing the value.
  74. *
  75. * @param bytes
  76. * @param start
  77. * @param length
  78. * a UTF-8 encoded string representation of a single byte quantity.
  79. * @param radix
  80. * the radix to use when parsing.
  81. * @return byte the value represented by the argument
  82. * @throws NumberFormatException
  83. * if the argument could not be parsed as a byte quantity.
  84. */
  85. public static byte parseByte(byte[] bytes, int start, int length, int radix) {
  86. int intValue = LazyInteger.parseInt(bytes, start, length, radix);
  87. byte result = (byte) intValue;
  88. if (result == intValue) {
  89. return result;
  90. }
  91. throw new NumberFormatException();
  92. }
  93. }