Search in sources :

Example 56 with ObjectOutput

use of java.io.ObjectOutput in project sessdb by ppdai.

the class TestUtil method getBytes.

public static byte[] getBytes(Object o) {
    if (o instanceof String) {
        return ((String) o).getBytes();
    } else if (o instanceof byte[]) {
        return (byte[]) o;
    } else if (o instanceof Serializable) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutput out = null;
        try {
            out = new ObjectOutputStream(bos);
            out.writeObject(o);
            byte[] bytes = bos.toByteArray();
            return bytes;
        } catch (Exception e) {
            return null;
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException ex) {
            // ignore close exception
            }
            try {
                bos.close();
            } catch (IOException ex) {
            // ignore close exception
            }
        }
    }
    throw new RuntimeException("Fail to convert object to bytes");
}
Also used : Serializable(java.io.Serializable) ObjectOutput(java.io.ObjectOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException)

Example 57 with ObjectOutput

use of java.io.ObjectOutput in project adempiere by adempiere.

the class MIssue method report.

//	getSystemStatus
/**************************************************************************
	 * 	Report/Update Issue.
	 *	@return error message
	 */
public String report() {
    if (true)
        return "-";
    StringBuffer parameter = new StringBuffer("?");
    if (//	don't report
    getRecord_ID() == 0)
        return "ID=0";
    if (//	new
    getRecord_ID() == 1) {
        parameter.append("ISSUE=");
        HashMap htOut = get_HashMap();
        try //	deserializing in create
        {
            ByteArrayOutputStream bOut = new ByteArrayOutputStream();
            ObjectOutput oOut = new ObjectOutputStream(bOut);
            oOut.writeObject(htOut);
            oOut.flush();
            String hexString = Secure.convertToHexString(bOut.toByteArray());
            parameter.append(hexString);
        } catch (Exception e) {
            log.severe(e.getLocalizedMessage());
            return "New-" + e.getLocalizedMessage();
        }
    } else //	existing
    {
        try {
            parameter.append("RECORDID=").append(getRecord_ID());
            parameter.append("&DBADDRESS=").append(URLEncoder.encode(getDBAddress(), "UTF-8"));
            parameter.append("&COMMENTS=").append(URLEncoder.encode(getComments(), "UTF-8"));
        } catch (Exception e) {
            log.severe(e.getLocalizedMessage());
            return "Update-" + e.getLocalizedMessage();
        }
    }
    InputStreamReader in = null;
    String target = "http://dev1/wstore/issueReportServlet";
    try //	Send GET Request
    {
        StringBuffer urlString = new StringBuffer(target).append(parameter);
        URL url = new URL(urlString.toString());
        URLConnection uc = url.openConnection();
        in = new InputStreamReader(uc.getInputStream());
    } catch (Exception e) {
        String msg = "Cannot connect to http://" + target;
        if (e instanceof FileNotFoundException || e instanceof ConnectException)
            msg += "\nServer temporarily down - Please try again later";
        else {
            msg += "\nCheck connection - " + e.getLocalizedMessage();
            log.log(Level.FINE, msg);
        }
        return msg;
    }
    return readResponse(in);
}
Also used : ObjectOutput(java.io.ObjectOutput) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) FileNotFoundException(java.io.FileNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ConnectException(java.net.ConnectException) FileNotFoundException(java.io.FileNotFoundException) URL(java.net.URL) URLConnection(java.net.URLConnection) ConnectException(java.net.ConnectException)

Example 58 with ObjectOutput

use of java.io.ObjectOutput in project geode by apache.

the class InternalDataSerializer method writeSerializableObject.

/**
   * write an object in java Serializable form with a SERIALIZABLE DSCODE so that it can be
   * deserialized with DataSerializer.readObject()
   * 
   * @param o the object to serialize
   * @param out the data output to serialize to
   */
public static void writeSerializableObject(Object o, DataOutput out) throws IOException {
    out.writeByte(SERIALIZABLE);
    if (out instanceof ObjectOutputStream) {
        ((ObjectOutputStream) out).writeObject(o);
    } else {
        OutputStream stream;
        if (out instanceof OutputStream) {
            stream = (OutputStream) out;
        } else {
            final DataOutput out2 = out;
            stream = new OutputStream() {

                @Override
                public void write(int b) throws IOException {
                    out2.write(b);
                }
            };
        }
        boolean wasDoNotCopy = false;
        if (out instanceof HeapDataOutputStream) {
            // To fix bug 52197 disable doNotCopy mode
            // while serialize with an ObjectOutputStream.
            // The problem is that ObjectOutputStream keeps
            // an internal byte array that it reuses while serializing.
            wasDoNotCopy = ((HeapDataOutputStream) out).setDoNotCopy(false);
        }
        try {
            ObjectOutput oos = new ObjectOutputStream(stream);
            if (stream instanceof VersionedDataStream) {
                Version v = ((VersionedDataStream) stream).getVersion();
                if (v != null && v != Version.CURRENT) {
                    oos = new VersionedObjectOutput(oos, v);
                }
            }
            oos.writeObject(o);
            // To fix bug 35568 just call flush. We can't call close because
            // it calls close on the wrapped OutputStream.
            oos.flush();
        } finally {
            if (wasDoNotCopy) {
                ((HeapDataOutputStream) out).setDoNotCopy(true);
            }
        }
    }
}
Also used : DataOutput(java.io.DataOutput) ObjectOutput(java.io.ObjectOutput) ObjectOutputStream(java.io.ObjectOutputStream) PdxOutputStream(org.apache.geode.pdx.internal.PdxOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) GemFireIOException(org.apache.geode.GemFireIOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 59 with ObjectOutput

use of java.io.ObjectOutput in project jdk8u_jdk by JetBrains.

the class Transport method serviceCall.

/**
     * Service an incoming remote call. When a message arrives on the
     * connection indicating the beginning of a remote call, the
     * threads are required to call the <I>serviceCall</I> method of
     * their transport.  The default implementation of this method
     * locates and calls the dispatcher object.  Ordinarily a
     * transport implementation will not need to override this method.
     * At the entry to <I>tr.serviceCall(conn)</I>, the connection's
     * input stream is positioned at the start of the incoming
     * message.  The <I>serviceCall</I> method processes the incoming
     * remote invocation and sends the result on the connection's
     * output stream.  If it returns "true", then the remote
     * invocation was processed without error and the transport can
     * cache the connection.  If it returns "false", a protocol error
     * occurred during the call, and the transport should destroy the
     * connection.
     */
public boolean serviceCall(final RemoteCall call) {
    try {
        /* read object id */
        final Remote impl;
        ObjID id;
        try {
            id = ObjID.read(call.getInputStream());
        } catch (java.io.IOException e) {
            throw new MarshalException("unable to read objID", e);
        }
        /* get the remote object */
        Transport transport = id.equals(dgcID) ? null : this;
        Target target = ObjectTable.getTarget(new ObjectEndpoint(id, transport));
        if (target == null || (impl = target.getImpl()) == null) {
            throw new NoSuchObjectException("no such object in table");
        }
        final Dispatcher disp = target.getDispatcher();
        target.incrementCallCount();
        try {
            /* call the dispatcher */
            transportLog.log(Log.VERBOSE, "call dispatcher");
            final AccessControlContext acc = target.getAccessControlContext();
            ClassLoader ccl = target.getContextClassLoader();
            ClassLoader savedCcl = Thread.currentThread().getContextClassLoader();
            try {
                setContextClassLoader(ccl);
                currentTransport.set(this);
                try {
                    java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction<Void>() {

                        public Void run() throws IOException {
                            checkAcceptPermission(acc);
                            disp.dispatch(impl, call);
                            return null;
                        }
                    }, acc);
                } catch (java.security.PrivilegedActionException pae) {
                    throw (IOException) pae.getException();
                }
            } finally {
                setContextClassLoader(savedCcl);
                currentTransport.set(null);
            }
        } catch (IOException ex) {
            transportLog.log(Log.BRIEF, "exception thrown by dispatcher: ", ex);
            return false;
        } finally {
            target.decrementCallCount();
        }
    } catch (RemoteException e) {
        // if calls are being logged, write out exception
        if (UnicastServerRef.callLog.isLoggable(Log.BRIEF)) {
            // include client host name if possible
            String clientHost = "";
            try {
                clientHost = "[" + RemoteServer.getClientHost() + "] ";
            } catch (ServerNotActiveException ex) {
            }
            String message = clientHost + "exception: ";
            UnicastServerRef.callLog.log(Log.BRIEF, message, e);
        }
        /* We will get a RemoteException if either a) the objID is
             * not readable, b) the target is not in the object table, or
             * c) the object is in the midst of being unexported (note:
             * NoSuchObjectException is thrown by the incrementCallCount
             * method if the object is being unexported).  Here it is
             * relatively safe to marshal an exception to the client
             * since the client will not have seen a return value yet.
             */
        try {
            ObjectOutput out = call.getResultStream(false);
            UnicastServerRef.clearStackTraces(e);
            out.writeObject(e);
            call.releaseOutputStream();
        } catch (IOException ie) {
            transportLog.log(Log.BRIEF, "exception thrown marshalling exception: ", ie);
            return false;
        }
    }
    return true;
}
Also used : MarshalException(java.rmi.MarshalException) ObjectOutput(java.io.ObjectOutput) Remote(java.rmi.Remote) IOException(java.io.IOException) IOException(java.io.IOException) Dispatcher(sun.rmi.server.Dispatcher) ServerNotActiveException(java.rmi.server.ServerNotActiveException) AccessControlContext(java.security.AccessControlContext) ObjID(java.rmi.server.ObjID) NoSuchObjectException(java.rmi.NoSuchObjectException) RemoteException(java.rmi.RemoteException)

Example 60 with ObjectOutput

use of java.io.ObjectOutput in project cogcomp-nlp by CogComp.

the class Serializer method write.

// public static void writeKryo(Object ob, String outputFile){
// try {
// kryo.writeObject(new Output(new FileOutputStream(outputFile)), ob);
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
// }
public static void write(Serializable ob, File outputCheck) {
    try {
        Serializable o = ob;
        if (outputCheck.getParentFile() != null)
            outputCheck.getParentFile().mkdirs();
        outputCheck.createNewFile();
        ObjectOutput output = getOutput(outputCheck.getPath());
        try {
            output.writeObject(o);
        } finally {
            output.close();
        }
    } catch (IOException ex) {
        System.err.println("Cannot write to output." + ex);
    }
}
Also used : Serializable(java.io.Serializable) ObjectOutput(java.io.ObjectOutput) IOException(java.io.IOException)

Aggregations

ObjectOutput (java.io.ObjectOutput)77 ObjectOutputStream (java.io.ObjectOutputStream)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)46 IOException (java.io.IOException)33 ObjectInput (java.io.ObjectInput)25 Test (org.junit.Test)20 ObjectInputStream (java.io.ObjectInputStream)15 ByteArrayInputStream (java.io.ByteArrayInputStream)14 WorkingMemory (org.drools.core.WorkingMemory)13 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)12 Pattern (org.drools.core.rule.Pattern)12 Consequence (org.drools.core.spi.Consequence)12 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)12 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 Declaration (org.drools.core.rule.Declaration)8 IntrospectionException (java.beans.IntrospectionException)7 InvalidRuleException (org.drools.core.rule.InvalidRuleException)7 ConsequenceException (org.drools.core.spi.ConsequenceException)7 OutputStream (java.io.OutputStream)6 ClassObjectType (org.drools.core.base.ClassObjectType)6