00001 /* 00002 ** Author(s): Miguel Calejo 00003 ** Contact: interprolog@declarativa.com, http://www.declarativa.com 00004 ** Copyright (C) Declarativa, Portugal, 2000-2002 00005 ** Use and distribution, without any warranties, under the terms of the 00006 ** GNU Library General Public License, readable in http://www.fsf.org/copyleft/lgpl.html 00007 */ 00008 package com.declarativa.interprolog.util; 00009 import java.io.*; 00012 public class PrologOutputObjectStream { 00013 OutputStream os; 00014 ObjectOutputStream tempObjects; 00015 ByteArrayOutputStream serializedTemp; 00016 boolean flushed = false; 00017 public PrologOutputObjectStream(OutputStream os) throws IOException{ 00018 this.os=os; 00019 serializedTemp = new ByteArrayOutputStream(); 00020 tempObjects = new ObjectOutputStream(serializedTemp); 00021 } 00022 00023 public ObjectOutputStream getObjectStream(){ 00024 return tempObjects; 00025 } 00026 00027 public void flush() throws IOException{ 00028 tempObjects.close(); 00029 (new DataOutputStream(os)).writeInt(size()); // byte count up front... 00030 serializedTemp.writeTo(os); 00031 serializedTemp.close(); 00032 os.flush(); 00033 flushed=true; 00034 } 00035 public void writeObject(Object obj) throws IOException{ 00036 if (flushed) throw new Error("A PrologOutputObjectStream can be used only once."); 00037 tempObjects.writeObject(obj); 00038 } 00039 public int size(){ 00040 return serializedTemp.size(); 00041 } 00042 }