com.alachisoft.ncache.serialization.standard.io.surrogates.StringSerializationSurrogate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nc-serialization Show documentation
Show all versions of nc-serialization Show documentation
Internal package of Alachisoft.
/*
* @(#)StringSerializationSurrogate.java 1.0
*
* Created on September 18, 2008, 12:59 PM
*
* Copyright 2008 NeXtreme Innovations, Inc. All rights reserved.
* "NeXtreme Innovations" PROPRIETARY/CONFIDENTIAL. Use is subject
* to license terms.
*/
package com.alachisoft.ncache.serialization.standard.io.surrogates;
import com.alachisoft.ncache.serialization.core.io.BlockDataInputStream;
import com.alachisoft.ncache.serialization.core.io.BlockDataOutputStream;
import com.alachisoft.ncache.serialization.core.io.NCacheObjectInput;
import com.alachisoft.ncache.serialization.core.io.NCacheObjectOutput;
import com.alachisoft.ncache.serialization.core.io.surrogates.*;
import java.io.IOException;
/**
* StringSerializationSurrogate is responsible for writing and reading
* instances of String class.
*
* @version 1.0, September 18, 2008
*/
public class StringSerializationSurrogate
extends SerializationSurrogateBase
implements SerializationSurrogate, BuiltinSerializationSurrogate {
/**
* Creates a new instance of StringSerializationSurrogate
*/
public StringSerializationSurrogate() {
super(String.class);
}
/**
* Read and return an object. The class that implements this interface
* defines where the object is "read" from.
*
* @param input the stream to read data from in order to restore the object.
* @return the object read from the stream
* @throws java.lang.ClassNotFoundException If the class of a serialized
* object cannot be found.
* @throws NCacheIOException If any of the usual Input/Output
* related exceptions occur.
*/
public Object readObject(NCacheObjectInput input)
throws NCacheInstantiationException, NCacheIOException {
BlockDataInputStream bin = (BlockDataInputStream) input.getContext().getUserItem("__bin");
try {
if (bin != null) {
return bin.readUTF();
} else {
return input.readUTF();
}
} catch (IOException ex) {
throw new NCacheIOException(ex);
}
}
/**
* Write an object to the underlying storage or stream. The
* class that implements this interface defines how the object is
* written.
*
* @param output the stream to write the object to.
* @param graph the object to write .
* @throws NCacheIOException Any of the usual Input/Output related exceptions.
*/
public void writeObject(NCacheObjectOutput output, Object graph)
throws NCacheIOException {
BlockDataOutputStream bout = (BlockDataOutputStream) output.getContext().getUserItem("__bout");
try {
if (bout != null) {
bout.writeUTF((String) graph);
} else {
output.writeUTF((String) graph);
}
} catch (IOException ex) {
throw new NCacheIOException(ex);
}
}
public void skipObject(NCacheObjectInput input) throws NCacheInstantiationException, NCacheIOException {
try {
input.skipUTF();
} catch (IOException ex) {
throw new NCacheIOException(ex);
}
}
}