/* 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;
/**
* This is a {@link Serializer} for {@link java.lang.Boolean} values. Every
* {@code boolean} value serialized by this object will be represented by a byte
* array of length one.
* <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 java.lang.Boolean
*/
public class BooleanSerializer implements Serializer<Boolean>
{
/**
* The singleton instance. May be used instead of instantiating this class.
*/
public static final BooleanSerializer INSTANCE = new BooleanSerializer();
/**
* One byte.
*/
public static final int DATA_SIZE = 1;
public Boolean interpret(byte[] barr, int offset, int length)
{
if (length != DATA_SIZE)
{
throw new SerializationException("Invalid length " + length + ". It must be one byte.");
}
return Boolean.valueOf(barr[offset] != 0);
}
public Boolean interpret(byte[] barr)
{
if (barr.length != DATA_SIZE)
{
throw new SerializationException("The size of the array must be one byte");
}
return Boolean.valueOf(barr[0] != 0);
}
public byte[] serialize(Boolean value)
{
if (value == null)
{
throw new NullPointerException("This serializer does not support null values");
}
byte[] res = new byte[1];
res[0] = value.booleanValue() ? (byte) 1 : (byte) 0;
return res;
}
public int serialize(Boolean value, byte[] barr, int offset)
{
if (value == null)
{
throw new NullPointerException("This serializer does not support null values");
}
barr[offset] = value.booleanValue() ? (byte) 1 : (byte) 0;
return 1;
}
/**
* @return {@value #DATA_SIZE}, always.
*/
public int getSerializedSize()
{
return DATA_SIZE;
}
/**
* @return {@code false}, always.
*/
public boolean isNullValuesPermitted()
{
return false;
}
/**
* Read a {@code Boolean} 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 Boolean} value.
* @throws NotEnoughDataException If {@value #DATA_SIZE} bytes cannot be
* read.
* @throws WrappedIOException On I/O errors
*/
public Boolean readBoolean(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 a {@code Boolean} 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 Boolean} value.
* @throws NotEnoughDataException If {@value #DATA_SIZE} bytes cannot be
* read.
* @throws WrappedIOException If an {@code IOException} is encountered while
* reading data.
*/
public Boolean readBoolean(InputStream is) throws WrappedIOException, NotEnoughDataException
{
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 IllegalArgumentException("Invalid data size " + dataSize + " bytes. It must be " + DATA_SIZE);
}
}
public Boolean read(RandomAccess ra, int dataSize)
{
validateDataSize(dataSize);
return readBoolean(ra);
}
public Boolean read(InputStream is, int dataSize)
{
validateDataSize(dataSize);
return readBoolean(is);
}
}