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