Search in sources :

Example 96 with ObjectOutputStream

use of java.io.ObjectOutputStream in project orientdb by orientechnologies.

the class OGremlinHelper method cloneObject.

/*
   * Tries to clone any Java object by using 3 techniques: - instanceof (most verbose but faster performance) - reflection (medium
   * performance) - serialization (applies for any object type but has a performance overhead)
   */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object cloneObject(final Object objectToClone, final Object previousClone) {
    // Clone any Map (shallow clone should be enough at this level)
    if (objectToClone instanceof Map) {
        Map recycledMap = (Map) previousClone;
        if (recycledMap == null)
            recycledMap = new HashMap();
        else
            recycledMap.clear();
        recycledMap.putAll((Map<?, ?>) objectToClone);
        return recycledMap;
    // Clone any collection (shallow clone should be enough at this level)
    } else if (objectToClone instanceof Collection) {
        Collection recycledCollection = (Collection) previousClone;
        if (recycledCollection == null)
            recycledCollection = new ArrayList();
        else
            recycledCollection.clear();
        recycledCollection.addAll((Collection<?>) objectToClone);
        return recycledCollection;
    // Clone String
    } else if (objectToClone instanceof String) {
        return objectToClone;
    } else if (objectToClone instanceof Number) {
        return objectToClone;
    // Clone Date
    } else if (objectToClone instanceof Date) {
        return (Date) ((Date) objectToClone).clone();
    } else {
        // ***************************************************************************************************************************************
        try {
            Object newClone;
            for (Class<?> obj = objectToClone.getClass(); !obj.equals(Object.class); obj = obj.getSuperclass()) {
                Method[] m = obj.getDeclaredMethods();
                for (int i = 0; i < m.length; i++) {
                    if (m[i].getName().equals("clone")) {
                        m[i].setAccessible(true);
                        newClone = m[i].invoke(objectToClone);
                        System.out.println(objectToClone.getClass() + " cloned by Reflection. Performance can be improved by adding the class to the list of known types");
                        return newClone;
                    }
                }
            }
            throw new Exception("Method clone not found");
        // ***************************************************************************************************************************************
        // 3. Polymorphic clone (Deep cloning by Serialization)
        // ***************************************************************************************************************************************
        } catch (Throwable e1) {
            try {
                final ByteArrayOutputStream bytes = new ByteArrayOutputStream() {

                    public synchronized byte[] toByteArray() {
                        return buf;
                    }
                };
                final ObjectOutputStream out = new ObjectOutputStream(bytes);
                out.writeObject(objectToClone);
                out.close();
                final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
                System.out.println(objectToClone.getClass() + " cloned by Serialization. Performance can be improved by adding the class to the list of known types");
                return in.readObject();
            // ***************************************************************************************************************************************
            // 4. Impossible to clone
            // ***************************************************************************************************************************************
            } catch (Throwable e2) {
                OLogManager.instance().error(null, "[GremlinHelper] error on cloning object %s, previous %s", e2, objectToClone, previousClone);
                return null;
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Date(java.util.Date) OException(com.orientechnologies.common.exception.OException) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) ByteArrayInputStream(java.io.ByteArrayInputStream) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) ObjectInputStream(java.io.ObjectInputStream)

Example 97 with ObjectOutputStream

use of java.io.ObjectOutputStream in project PocketHub by pockethub.

the class RequestWriter method write.

/**
     * Write request to file
     *
     * @param request
     * @return request
     */
public <V> V write(V request) {
    RandomAccessFile dir = null;
    FileLock lock = null;
    ObjectOutputStream output = null;
    try {
        createDirectory(handle.getParentFile());
        dir = new RandomAccessFile(handle, "rw");
        lock = dir.getChannel().lock();
        output = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(dir.getFD()), 8192));
        output.writeInt(version);
        output.writeObject(request);
    } catch (IOException e) {
        Log.d(TAG, "Exception writing cache " + handle.getName(), e);
        return null;
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                Log.d(TAG, "Exception closing stream", e);
            }
        }
        if (lock != null) {
            try {
                lock.release();
            } catch (IOException e) {
                Log.d(TAG, "Exception unlocking file", e);
            }
        }
        if (dir != null) {
            try {
                dir.close();
            } catch (IOException e) {
                Log.d(TAG, "Exception closing file", e);
            }
        }
    }
    return request;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 98 with ObjectOutputStream

use of java.io.ObjectOutputStream in project javatari by ppeccin.

the class RemoteReceiver method tryConnection.

private void tryConnection(String serverAddress) throws IOException, IllegalArgumentException {
    this.serverAddress = serverAddress;
    try {
        String addr = getHost(serverAddress);
        int port = getPort(serverAddress);
        socket = new Socket(addr, port);
        socket.setTcpNoDelay(true);
        socketOutputStream = socket.getOutputStream();
        outputStream = new ObjectOutputStream(socketOutputStream);
        socketInputStream = socket.getInputStream();
        inputStream = new ObjectInputStream(socketInputStream);
    } catch (IOException ex) {
        disconnection();
        throw ex;
    }
    resetUpdatesPending();
    updatesReceiver = new UpdatesReceiver();
    updatesReceiver.start();
    updatesConsumer = new UpdatesConsumer();
    updatesConsumer.start();
    console.connected();
    notifyConnectionStatusListeners();
}
Also used : IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Socket(java.net.Socket) ObjectInputStream(java.io.ObjectInputStream)

Example 99 with ObjectOutputStream

use of java.io.ObjectOutputStream in project javatari by ppeccin.

the class RemoteTransmitter method connect.

private void connect(Socket toSocket) throws IOException {
    socket = toSocket;
    socket.setTcpNoDelay(true);
    socketOutputStream = socket.getOutputStream();
    outputStream = new ObjectOutputStream(socketOutputStream);
    socketInputStream = socket.getInputStream();
    inputStream = new ObjectInputStream(socketInputStream);
    resetUpdatesPending();
    console.clientConnected();
    notifyConnectionStatusListeners();
}
Also used : ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 100 with ObjectOutputStream

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

the class AbstractSortedMapTable method persistBloomFilter.

public void persistBloomFilter() throws IOException {
    ensureNotClosed();
    File file = new File(this.bloomFilterFile);
    if (!file.exists()) {
        file.createNewFile();
    }
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    try {
        fos = new FileOutputStream(file);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(this.bloomFilter);
        oos.flush();
    } finally {
        oos.close();
        fos.close();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File)

Aggregations

ObjectOutputStream (java.io.ObjectOutputStream)958 ByteArrayOutputStream (java.io.ByteArrayOutputStream)657 ObjectInputStream (java.io.ObjectInputStream)386 ByteArrayInputStream (java.io.ByteArrayInputStream)332 IOException (java.io.IOException)312 FileOutputStream (java.io.FileOutputStream)132 Test (org.junit.Test)130 File (java.io.File)75 BufferedOutputStream (java.io.BufferedOutputStream)46 ObjectOutput (java.io.ObjectOutput)35 OutputStream (java.io.OutputStream)35 HashMap (java.util.HashMap)35 FileInputStream (java.io.FileInputStream)24 ArrayList (java.util.ArrayList)24 InputStream (java.io.InputStream)22 FileNotFoundException (java.io.FileNotFoundException)20 Serializable (java.io.Serializable)15 Test (org.testng.annotations.Test)15 NotSerializableException (java.io.NotSerializableException)14 Map (java.util.Map)13