001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal.handlers;
022:
023: import com.db4o.*;
024: import com.db4o.foundation.*;
025: import com.db4o.internal.*;
026: import com.db4o.marshall.ReadContext;
027: import com.db4o.marshall.WriteContext;
028: import com.db4o.reflect.*;
029:
030: public final class ByteHandler extends PrimitiveHandler {
031:
032: static final int LENGTH = 1 + Const4.ADDED_LENGTH;
033:
034: private static final Byte DEFAULT_VALUE = new Byte((byte) 0);
035:
036: public ByteHandler(ObjectContainerBase stream) {
037: super (stream);
038: }
039:
040: public Object coerce(ReflectClass claxx, Object obj) {
041: return Coercion4.toSByte(obj);
042: }
043:
044: public Object defaultValue() {
045: return DEFAULT_VALUE;
046: }
047:
048: public int linkLength() {
049: return LENGTH;
050: }
051:
052: protected Class primitiveJavaClass() {
053: return byte.class;
054: }
055:
056: public Object primitiveNull() {
057: return DEFAULT_VALUE;
058: }
059:
060: Object read1(Buffer a_bytes) {
061: if (Deploy.debug) {
062: a_bytes.readBegin(Const4.YAPBYTE);
063: }
064: byte ret = a_bytes.readByte();
065: if (Deploy.debug) {
066: a_bytes.readEnd();
067: }
068: return new Byte(ret);
069: }
070:
071: public void write(Object a_object, Buffer a_bytes) {
072: if (Deploy.debug) {
073: a_bytes.writeBegin(Const4.YAPBYTE);
074: }
075: a_bytes.writeByte(((Byte) a_object).byteValue());
076: if (Deploy.debug) {
077: a_bytes.writeEnd();
078: }
079: }
080:
081: // Comparison_______________________
082:
083: private byte i_compareTo;
084:
085: private byte val(Object obj) {
086: return ((Byte) obj).byteValue();
087: }
088:
089: void prepareComparison1(Object obj) {
090: i_compareTo = val(obj);
091: }
092:
093: boolean isEqual1(Object obj) {
094: return obj instanceof Byte && val(obj) == i_compareTo;
095: }
096:
097: boolean isGreater1(Object obj) {
098: return obj instanceof Byte && val(obj) > i_compareTo;
099: }
100:
101: boolean isSmaller1(Object obj) {
102: return obj instanceof Byte && val(obj) < i_compareTo;
103: }
104:
105: public Object read(ReadContext context) {
106: if (Deploy.debug) {
107: Debug.readBegin(context, Const4.YAPBYTE);
108: }
109:
110: byte byteValue = context.readByte();
111:
112: if (Deploy.debug) {
113: Debug.readEnd(context);
114: }
115:
116: return new Byte(byteValue);
117: }
118:
119: public void write(WriteContext context, Object obj) {
120: if (Deploy.debug) {
121: Debug.writeBegin(context, Const4.YAPBYTE);
122: }
123:
124: context.writeByte(((Byte) obj).byteValue());
125:
126: if (Deploy.debug) {
127: Debug.writeEnd(context);
128: }
129: }
130: }
|