at.spardat.xma.serializer.BinarySerializer Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
//@(#) $Id: BinarySerializer.java 2089 2007-11-28 13:56:13Z s3460 $
package at.spardat.xma.serializer;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import at.spardat.enterprise.exc.SysException;
import at.spardat.xma.util.ByteArray;
/**
* Serializer implementation that uses the Java-Serialization mechanism
*
* @author YSD, 11.02.2005
*/
public final class BinarySerializer implements Serializer {
/**
* Is this executing at the server side of xma?
*/
private boolean fIsAtServer = false;
/**
* This member is accessed from methods implementing the Synchronization
* interface.
*/
private ObjectOutput fObjOut_;
/**
* The output buffer
*/
private ByteArray fBuf;
/**
* Constructor
*
* @param isAtServer is this serializer running at the server side?
* @param bufferSize initial buffer size
*/
public BinarySerializer (boolean isAtServer, int bufferSize) {
fIsAtServer = isAtServer;
fBuf = new ByteArray (bufferSize);
}
/**
* Since the streams cannot be opened in the constructor, we
* do it lazy in this method
*/
private final void initialize () {
if (fObjOut_ == null) {
try {
fObjOut_ = new ObjectOutputStream (fBuf.getOutputStream());
} catch (IOException x) {
throw new SysException (x);
}
}
}
/**
* @see at.spardat.xma.serializer.Serializer#addHeader()
*/
public void addHeader() {
fBuf.addHeader();
}
/**
* @see at.spardat.xma.serializer.Serializer#isAtServer()
*/
public boolean isAtServer() {
return fIsAtServer;
}
/**
* @see at.spardat.xma.serializer.Serializer#getResult()
*/
public ByteArray getResult() {
if (fObjOut_ == null) initialize();
try {
fObjOut_.close();
} catch (Exception x) {
throw new SysException (x);
}
return fBuf;
}
/* (non-Javadoc)
* @see at.spardat.xma.serializer.XmaOutput#writeString(java.lang.String, java.lang.String)
*/
public void writeString (String label, String s) throws IOException {
if (fObjOut_ == null) initialize();
fObjOut_.writeUTF(s);
}
/**
* @see at.spardat.xma.serializer.XmaOutput#writeStringN(java.lang.String, java.lang.String)
*/
public void writeStringN (String label, String s) throws IOException {
if (fObjOut_ == null) initialize();
fObjOut_.writeBoolean(s == null);
if (s != null) {
fObjOut_.writeUTF(s);
}
}
/* (non-Javadoc)
* @see at.spardat.xma.serializer.XmaOutput#writeInt(java.lang.String, int)
*/
public void writeInt(String label, int i) throws IOException {
if (fObjOut_ == null) initialize();
fObjOut_.writeInt(i);
}
/* (non-Javadoc)
* @see at.spardat.xma.serializer.XmaOutput#writeLong(java.lang.String, long)
*/
public void writeLong(String label, long l) throws IOException {
if (fObjOut_ == null) initialize();
fObjOut_.writeLong(l);
}
/* (non-Javadoc)
* @see at.spardat.xma.serializer.XmaOutput#writeShort(java.lang.String, short)
*/
public void writeShort(String label, int s) throws IOException {
if (fObjOut_ == null) initialize();
fObjOut_.writeShort(s);
}
/* (non-Javadoc)
* @see at.spardat.xma.serializer.XmaOutput#writeBoolean(java.lang.String, boolean)
*/
public void writeBoolean(String label, boolean b) throws IOException {
if (fObjOut_ == null) initialize();
fObjOut_.writeBoolean(b);
}
/* (non-Javadoc)
* @see at.spardat.xma.serializer.XmaOutput#writeByte(java.lang.String, int)
*/
public void writeByte(String label, int b) throws IOException {
if (fObjOut_ == null) initialize();
fObjOut_.writeByte(b);
}
/**
* @see at.spardat.xma.serializer.XmaOutput#writeSerializedBytes(String, byte[])
*/
public void writeSerializedBytes(String label, byte[] bytes) throws IOException {
if (fObjOut_ == null) initialize();
fObjOut_.writeInt (bytes.length);
fObjOut_.write (bytes);
}
/* (non-Javadoc)
* @see at.spardat.xma.serializer.XmaOutput#writeObject(java.lang.String, java.lang.Object)
*/
public void writeObject(String label, Object obj) throws IOException {
if (fObjOut_ == null) initialize();
fObjOut_.writeObject(obj);
}
}