Search in sources :

Example 11 with ObjectInputStream

use of java.io.ObjectInputStream in project flink by apache.

the class ZooKeeperLeaderElectionService method nodeChanged.

@Override
public void nodeChanged() throws Exception {
    try {
        // leaderSessionID is null if the leader contender has not yet confirmed the session ID
        if (leaderLatch.hasLeadership()) {
            synchronized (lock) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Leader node changed while {} is the leader with session ID {}.", leaderContender.getAddress(), confirmedLeaderSessionID);
                }
                if (confirmedLeaderSessionID != null) {
                    ChildData childData = cache.getCurrentData();
                    if (childData == null) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Writing leader information into empty node by {}.", leaderContender.getAddress());
                        }
                        writeLeaderInformation(confirmedLeaderSessionID);
                    } else {
                        byte[] data = childData.getData();
                        if (data == null || data.length == 0) {
                            // the data field seems to be empty, rewrite information
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("Writing leader information into node with empty data field by {}.", leaderContender.getAddress());
                            }
                            writeLeaderInformation(confirmedLeaderSessionID);
                        } else {
                            ByteArrayInputStream bais = new ByteArrayInputStream(data);
                            ObjectInputStream ois = new ObjectInputStream(bais);
                            String leaderAddress = ois.readUTF();
                            UUID leaderSessionID = (UUID) ois.readObject();
                            if (!leaderAddress.equals(this.leaderContender.getAddress()) || (leaderSessionID == null || !leaderSessionID.equals(confirmedLeaderSessionID))) {
                                // the data field does not correspond to the expected leader information
                                if (LOG.isDebugEnabled()) {
                                    LOG.debug("Correcting leader information by {}.", leaderContender.getAddress());
                                }
                                writeLeaderInformation(confirmedLeaderSessionID);
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        leaderContender.handleError(new Exception("Could not handle node changed event.", e));
        throw e;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ChildData(org.apache.curator.framework.recipes.cache.ChildData) UUID(java.util.UUID) KeeperException(org.apache.zookeeper.KeeperException) ObjectInputStream(java.io.ObjectInputStream)

Example 12 with ObjectInputStream

use of java.io.ObjectInputStream in project flink by apache.

the class SharedBufferTest method testSharedBufferSerialization.

@Test
public void testSharedBufferSerialization() throws IOException, ClassNotFoundException {
    SharedBuffer<String, Event> sharedBuffer = new SharedBuffer<>(Event.createTypeSerializer());
    int numberEvents = 8;
    Event[] events = new Event[numberEvents];
    final long timestamp = 1L;
    for (int i = 0; i < numberEvents; i++) {
        events[i] = new Event(i + 1, "e" + (i + 1), i);
    }
    sharedBuffer.put("a1", events[0], timestamp, null, null, 0, DeweyNumber.fromString("1"));
    sharedBuffer.put("a[]", events[1], timestamp, "a1", events[0], timestamp, DeweyNumber.fromString("1.0"));
    sharedBuffer.put("a1", events[2], timestamp, null, null, 0, DeweyNumber.fromString("2"));
    sharedBuffer.put("a[]", events[2], timestamp, "a[]", events[1], timestamp, DeweyNumber.fromString("1.0"));
    sharedBuffer.put("a[]", events[3], timestamp, "a[]", events[2], timestamp, DeweyNumber.fromString("1.0"));
    sharedBuffer.put("a[]", events[3], timestamp, "a1", events[2], timestamp, DeweyNumber.fromString("2.0"));
    sharedBuffer.put("a[]", events[4], timestamp, "a[]", events[3], timestamp, DeweyNumber.fromString("1.0"));
    sharedBuffer.put("a[]", events[5], timestamp, "a[]", events[4], timestamp, DeweyNumber.fromString("1.1"));
    sharedBuffer.put("b", events[5], timestamp, "a[]", events[3], timestamp, DeweyNumber.fromString("2.0.0"));
    sharedBuffer.put("b", events[5], timestamp, "a[]", events[4], timestamp, DeweyNumber.fromString("1.0.0"));
    sharedBuffer.put("a[]", events[6], timestamp, "a[]", events[5], timestamp, DeweyNumber.fromString("1.1"));
    sharedBuffer.put("b", events[7], timestamp, "a[]", events[6], timestamp, DeweyNumber.fromString("1.1.0"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(sharedBuffer);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bais);
    SharedBuffer<String, Event> copy = (SharedBuffer<String, Event>) ois.readObject();
    assertEquals(sharedBuffer, copy);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Event(org.apache.flink.cep.Event) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 13 with ObjectInputStream

use of java.io.ObjectInputStream in project ansj_seg by NLPchina.

the class MyStaticValue method getPersonFreqMap.

/**
	 * 名字词性对象反序列化
	 * 
	 * @return
	 */
@SuppressWarnings("unchecked")
public static Map<String, int[][]> getPersonFreqMap() {
    Map<String, int[][]> map = new HashMap<String, int[][]>(0);
    try (InputStream inputStream = DicReader.getInputStream("person/asian_name_freq.data")) {
        ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
        map = (Map<String, int[][]>) objectInputStream.readObject();
    } catch (IOException e) {
        LOG.warn("IO异常", e);
    } catch (ClassNotFoundException e) {
        LOG.warn("找不到类", e);
    }
    return map;
}
Also used : HashMap(java.util.HashMap) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 14 with ObjectInputStream

use of java.io.ObjectInputStream in project android_frameworks_base by ParanoidAndroid.

the class NinePatch_Delegate method getChunk.

/**
     * Returns a {@link NinePatchChunk} object for the given serialized representation.
     *
     * If the chunk is present in the cache then the object from the cache is returned, otherwise
     * the array is deserialized into a {@link NinePatchChunk} object.
     *
     * @param array the serialized representation of the chunk.
     * @return the NinePatchChunk or null if deserialization failed.
     */
public static NinePatchChunk getChunk(byte[] array) {
    SoftReference<NinePatchChunk> chunkRef = sChunkCache.get(array);
    NinePatchChunk chunk = chunkRef.get();
    if (chunk == null) {
        ByteArrayInputStream bais = new ByteArrayInputStream(array);
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(bais);
            chunk = (NinePatchChunk) ois.readObject();
            // put back the chunk in the cache
            if (chunk != null) {
                sChunkCache.put(array, new SoftReference<NinePatchChunk>(chunk));
            }
        } catch (IOException e) {
            Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Failed to deserialize NinePatchChunk content.", e, null);
            return null;
        } catch (ClassNotFoundException e) {
            Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Failed to deserialize NinePatchChunk class.", e, null);
            return null;
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                }
            }
        }
    }
    return chunk;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) NinePatchChunk(com.android.ninepatch.NinePatchChunk) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 15 with ObjectInputStream

use of java.io.ObjectInputStream in project android_frameworks_base by ParanoidAndroid.

the class PrintPsTree method main.

public static void main(String[] args) throws IOException, ClassNotFoundException {
    if (args.length != 1) {
        System.err.println("Usage: PrintCsv [compiled log file]");
        System.exit(0);
    }
    FileInputStream fin = new FileInputStream(args[0]);
    ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(fin));
    Root root = (Root) oin.readObject();
    for (Proc proc : root.processes.values()) {
        if (proc.parent == null) {
            proc.print();
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

ObjectInputStream (java.io.ObjectInputStream)2159 ByteArrayInputStream (java.io.ByteArrayInputStream)1406 ObjectOutputStream (java.io.ObjectOutputStream)970 ByteArrayOutputStream (java.io.ByteArrayOutputStream)807 IOException (java.io.IOException)752 Test (org.junit.Test)458 FileInputStream (java.io.FileInputStream)339 File (java.io.File)197 InputStream (java.io.InputStream)157 ArrayList (java.util.ArrayList)90 BufferedInputStream (java.io.BufferedInputStream)84 Serializable (java.io.Serializable)66 FileNotFoundException (java.io.FileNotFoundException)64 FileOutputStream (java.io.FileOutputStream)63 HashMap (java.util.HashMap)63 Socket (java.net.Socket)49 Map (java.util.Map)48 ObjectInput (java.io.ObjectInput)45 GZIPInputStream (java.util.zip.GZIPInputStream)44 List (java.util.List)37