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");
}
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);
}
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);
}
}
}
}
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;
}
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);
}
}
Aggregations