Package org.helidb.lang.serializer

Source Code of org.helidb.lang.serializer.IntegerSerializer

/* HeliDB -- A simple database for Java, http://www.helidb.org
* Copyright (C) 2008, 2009 Karl Gustafsson
*
* This file is a part of HeliDB.
*
* HeliDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeliDB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package org.helidb.lang.serializer;

import java.io.IOException;
import java.io.InputStream;

import org.entityfs.RandomAccess;
import org.entityfs.support.exception.WrappedIOException;
import org.helidb.io.NotEnoughDataException;

/**
* A {@link Serializer} for {@link java.lang.Integer} values. {@code Integer}
* values are serialized to a four bytes long, big-endian byte array.
* <p>
* This serializer does not handle {@code null} values.
* <p>
* The singleton instance {@link #INSTANCE} may be used instead of creating a
* new object of this class.
* @author Karl Gustafsson
* @since 1.0
* @see IntegerNullSerializer
* @see ConfigurableLengthIntegerSerializer
* @see java.lang.Integer
*/
public class IntegerSerializer implements Serializer<Integer>
{
  /**
   * The singleton instance. May be used instead of instantiating this class.
   */
  public static final IntegerSerializer INSTANCE = new IntegerSerializer();

  /**
   * Four bytes.
   */
  public static final int DATA_SIZE = 4;

  /**
   * Get the {@code int} value encoded in the big-endian, byte array.
   * @param barr The byte array. It must be four bytes long and big-endian.
   * @return The int value encoded in the array.
   * @throws SerializationException If the array length is different from four
   * bytes.
   * @throws NullPointerException If {@code barr} is {@code null}
   * @see #encodeInteger(int)
   */
  public static int getInteger(byte[] barr) throws SerializationException, NullPointerException
  {
    if (barr.length != DATA_SIZE)
    {
      throw new SerializationException("The size of the array must be four bytes");
    }
    return (barr[3] & 0xFF) + ((barr[2] & 0xFF) << 8) + ((barr[1] & 0xFF) << 16) + ((barr[0] & 0xFF) << 24);
  }

  public Integer interpret(byte[] barr, int offset, int length)
  {
    if (length != DATA_SIZE)
    {
      throw new SerializationException("Invalid length " + length + ". Must be " + DATA_SIZE + " bytes");
    }
    return (barr[3 + offset] & 0xFF) + ((barr[2 + offset] & 0xFF) << 8) + ((barr[1 + offset] & 0xFF) << 16) + ((barr[offset] & 0xFF) << 24);
  }

  public Integer interpret(byte[] barr)
  {
    return Integer.valueOf(getInteger(barr));
  }

  /**
   * Encode an integer to a four bytes long, big-endian byte array.
   * @param i The integer to encode.
   * @return A four bytes long byte array, big-endian.
   */
  public static byte[] encodeInteger(int i)
  {
    byte[] res = new byte[4];
    res[0] = (byte) (i >> 24);
    res[1] = (byte) (i >> 16);
    res[2] = (byte) (i >> 8);
    res[3] = (byte) i;
    return res;
  }

  public int serialize(Integer value, byte[] barr, int offset)
  {
    if (value == null)
    {
      throw new NullPointerException("This serializer does not support null values");
    }
    int i = value.intValue();
    barr[offset] = (byte) (i >> 24);
    barr[offset + 1] = (byte) (i >> 16);
    barr[offset + 2] = (byte) (i >> 8);
    barr[offset + 3] = (byte) i;
    return DATA_SIZE;
  }

  public byte[] serialize(Integer value)
  {
    if (value == null)
    {
      throw new NullPointerException("This serializer does not support null values");
    }
    return encodeInteger(value.intValue());
  }

  /**
   * @return {@value #DATA_SIZE}, always.
   */
  public int getSerializedSize()
  {
    return DATA_SIZE;
  }

  /**
   * @return {@code false}, always.
   */
  public boolean isNullValuesPermitted()
  {
    return false;
  }

  /**
   * Read an {@code Integer} value from the current position of the {@code
   * RandomAccess}.
   * @param ra The {@code RandomAccess} to read from. The current position of
   * this is incremented by {@value #DATA_SIZE} bytes.
   * @return The {@code Integer} value.
   * @throws NotEnoughDataException If {@value #DATA_SIZE} bytes cannot be
   * read.
   * @throws WrappedIOException On I/O errors.
   */
  public Integer readInteger(RandomAccess ra) throws NotEnoughDataException, WrappedIOException
  {
    byte[] larr = new byte[DATA_SIZE];
    int noRead = ra.read(larr);
    if (noRead != DATA_SIZE)
    {
      throw new NotEnoughDataException(DATA_SIZE, noRead);
    }
    return interpret(larr);
  }

  /**
   * Read an {@code Integer} value from the current position of the {@code
   * InputStream}.
   * @param is The {@code InputStream} to read from. The current position of
   * the stream is incremented by {@value #DATA_SIZE} bytes.
   * @return The {@code Integer} value.
   * @throws NotEnoughDataException If {@value #DATA_SIZE} bytes cannot be
   * read.
   * @throws WrappedIOException If an {@code IOException} is encountered while
   * reading data.
   */
  public Integer readInteger(InputStream is) throws NotEnoughDataException, WrappedIOException
  {
    byte[] larr = new byte[DATA_SIZE];
    try
    {
      int noRead = is.read(larr);
      if (noRead != DATA_SIZE)
      {
        throw new NotEnoughDataException(DATA_SIZE, noRead);
      }
    }
    catch (IOException e)
    {
      throw new WrappedIOException(e);
    }
    return interpret(larr);
  }

  private void validateDataSize(int dataSize)
  {
    if (dataSize != DATA_SIZE)
    {
      throw new SerializationException("Invalid data size " + dataSize + " bytes. It must be " + DATA_SIZE);
    }
  }

  public Integer read(RandomAccess ra, int dataSize)
  {
    validateDataSize(dataSize);
    return readInteger(ra);
  }

  public Integer read(InputStream is, int dataSize)
  {
    validateDataSize(dataSize);
    return readInteger(is);
  }
}
TOP

Related Classes of org.helidb.lang.serializer.IntegerSerializer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.