public void writeExternal(ObjectOutput out) throws IOException { boolean isDrools = out instanceof DroolsObjectOutputStream; DroolsObjectOutputStream droolsStream; ByteArrayOutputStream bytes; if ( isDrools ) { bytes = null; droolsStream = (DroolsObjectOutputStream) out; } else { bytes = new ByteArrayOutputStream(); droolsStream = new DroolsObjectOutputStream( bytes ); } droolsStream.writeObject( rules ); droolsStream.writeObject( namedWindows ); droolsStream.writeObject( idGenerator ); droolsStream.writeBoolean( ordered ); if ( !isDrools ) { droolsStream.flush(); droolsStream.close(); bytes.close(); out.writeInt( bytes.size() ); out.writeObject( bytes.toByteArray() ); } }
/** * This method would stream out the given object to the given output stream uncompressed or compressed * depending on the given flag. The output contents could only be read by the corresponding "streamIn" * methods of this class. * @param out * @param object * @throws IOException */ public static void streamOut(OutputStream out, Object object, boolean compressed) throws IOException { if (compressed) { out = new GZIPOutputStream(out); } DroolsObjectOutputStream doos = null; try { doos = new DroolsObjectOutputStream(out); doos.writeObject(object); } finally { if( doos != null ) { doos.close(); } if (compressed) { out.close(); } } }
/** * Writes a Drools KnowledgePackage to File. * @param kpkg the KnowledgePackage * @param dest the File * @throws IOException oops */ public static void write(KnowledgePackage kpkg, File dest) throws IOException { ObjectOutputStream oos = null; try { oos = new DroolsObjectOutputStream(new BufferedOutputStream(new FileOutputStream(dest))); oos.writeObject(kpkg); } finally { if (oos != null) { try { oos.flush(); oos.close(); } catch (Throwable t) { // just to keep checkstyle happy t.getMessage(); } } } }
public static <T extends Externalizable> T deepClone(T origin, ClassLoader classLoader) { if (origin == null) { return null; } try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new DroolsObjectOutputStream(baos); oos.writeObject(origin); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new DroolsObjectInputStream(bais, classLoader); Object deepCopy = ois.readObject(); return (T)deepCopy; } catch(IOException ioe) { throw new RuntimeException(ioe); } catch (ClassNotFoundException cnfe) { throw new RuntimeException(cnfe); } }
/** * Handles the write serialization of the Package. Patterns in Rules may reference generated data which cannot be serialized by default methods. * The Package uses PackageCompilationData to hold a reference to the generated bytecode. The generated bytecode must be restored before any Rules. */ public void writeExternal(final ObjectOutput stream) throws IOException { DroolsObjectOutputStream droolsStream; boolean isDrools = stream instanceof DroolsObjectOutputStream; ByteArrayOutputStream bytes = null; stream.writeBoolean( isDrools ); if ( isDrools ) { droolsStream = (DroolsObjectOutputStream) stream; } else { bytes = new ByteArrayOutputStream(); droolsStream = new DroolsObjectOutputStream( bytes ); } super.writeExternal( droolsStream ); droolsStream.writeObject( this.reteooBuilder ); droolsStream.writeObject( this.rete ); if ( !isDrools ) { droolsStream.flush(); droolsStream.close(); bytes.close(); stream.writeObject( bytes.toByteArray() ); } }
private void writeLocalCacheCopy(Package p, URL u, File localCacheDir) { File local = getLocalCacheFileForURL( localCacheDir, u ); if (local.exists()) local.delete(); try { ObjectOutput out = new DroolsObjectOutputStream(new FileOutputStream(local)); out.writeObject( p ); out.flush(); out.close(); } catch (IOException e) { listener.exception( e ); listener.warning( "Was an error with the local cache directory " + localCacheDir.getPath() ); } }
} else { bytes = new ByteArrayOutputStream(); out = new DroolsObjectOutputStream( bytes );
} else { bytes = new ByteArrayOutputStream(); droolsStream = new DroolsObjectOutputStream( bytes );
private static void writeStrategiesIndex(MarshallerWriteContext context, ProtobufMessages.Header.Builder _header) throws IOException { for( Entry<ObjectMarshallingStrategy,Integer> entry : context.usedStrategies.entrySet() ) { Builder _strat = ProtobufMessages.Header.StrategyIndex.newBuilder() .setId( entry.getValue().intValue() ) .setName( entry.getKey().getClass().getName() ); Context ctx = context.strategyContext.get( entry.getKey() ); if( ctx != null ) { Output os = ByteString.newOutput(); ctx.write( new DroolsObjectOutputStream( os ) ); _strat.setData( os.toByteString() ); os.close(); } _header.addStrategy( _strat.build() ); } }