Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright (c) 2012 Jan Kotek
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *    http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package org.mapdb;
 
 
Provides serialization and deserialization

Author(s):
Jan Kotek
 
 public interface Serializer<A> {

    
Serialize the content of an object into a ObjectOutput

Parameters:
out ObjectOutput to save object into
value Object to serialize
 
     public void serializeDataOutput out, A value)
             throws IOException;


    
Deserialize the content of an object from a DataInput.

Parameters:
in to read serialized data from
available how many bytes are available in DataInput for reading, may be -1 (in streams) or 0 (null).
Returns:
deserialized object
Throws:
java.io.IOException
 
     public A deserializeDataInput inint available)
             throws IOException;

    
Serializes strings using UTF8 encoding. Used mainly for testing. Does not handle null values.
 
 
         @Override
 		public void serialize(DataOutput outString valuethrows IOException {
             final byte[] bytes = value.getBytes(.);
             out.write(bytes);
         }
 
 
         @Override
 		public String deserialize(DataInput inint availablethrows IOException {
             if(available==-1) throw new IllegalArgumentException("STRING_NOSIZE does not work with collections.");
             byte[] bytes = new byte[available];
             in.readFully(bytes);
             return new String(bytes.);
         }
     };





    
Serializes Long into 8 bytes, used mainly for testing. Does not handle null values
 
      
      Serializer<LongLONG = new Serializer<Long>() {
         @Override
         public void serialize(DataOutput outLong valuethrows IOException {
             if(value != null)
                 out.writeLong(value);
         }
 
         @Override
         public Long deserialize(DataInput inint availablethrows IOException {
             if(available==0) return null;
             return in.readLong();
         }
     };

    
Serializes Integer into 4 bytes, used mainly for testing. Does not handle null values
 
     
     Serializer<IntegerINTEGER = new Serializer<Integer>() {
         @Override
        public void serialize(DataOutput outInteger valuethrows IOException {
            out.writeInt(value);
        }
        @Override
        public Integer deserialize(DataInput inint availablethrows IOException {
            return in.readInt();
        }
    };
    
        @Override
        public void serialize(DataOutput outBoolean valuethrows IOException {
            out.writeBoolean(value);
        }
        @Override
        public Boolean deserialize(DataInput inint availablethrows IOException {
            if(available==0) return null;
            return in.readBoolean();
        }
    };

    


    
always writes zero length data, and always deserializes it as an empty String

Deprecated:
TODO remove
        @Override
        public void serialize(DataOutput outObject valuethrows IOException {
            assert(value==.);
        }
        @Override
        public Object deserialize(DataInput inint availablethrows IOException {
            assert(available>0);
            return .;
        }
    };

    
basic serializer for most classes in 'java.lang' and 'java.util' packages

Deprecated:
TODO remove
    @SuppressWarnings("unchecked")
    Serializer<ObjectBASIC = new SerializerBase();


    

Deprecated:
TODO remove
    Serializer<byte[] > BYTE_ARRAY_NOSIZE = new Serializer<byte[]>() {
        @Override
        public void serialize(DataOutput outbyte[] valuethrows IOException {
            if(value==null||value.length==0) return;
            out.write(value);
        }
        @Override
        public byte[] deserialize(DataInput inint availablethrows IOException {
            if(available==-1) throw new IllegalArgumentException("BYTE_ARRAY_NOSIZE does not work with collections.");
            if(available==0) return null;
            byte[] ret = new byte[available];
            in.readFully(ret);
            return ret;
        }
    } ;
New to GrepCode? Check out our FAQ X