Search in sources :

Example 6 with ObjectInputStream

use of java.io.ObjectInputStream in project pinot by linkedin.

the class GenericResultSetMapper method main.

public static void main(String[] args) throws Exception {
    ModelMapper mapper = new ModelMapper();
    Map<String, Object> result = new HashMap<>();
    //[{jobName=Test_Anomaly_Task, jobId=1, workerId=1, taskType=MONITOR, id=1, taskInfo=clob2: '{"jobExecutionId":1,"monitorType":"UPDATE","expireDaysAgo":0}', lastModified=2016-08-24 17:25:53.258, version=0, taskStartTime=1470356753227, status=RUNNING, taskEndTime=1471220753227}]
    result.put("jobName", "Test_Anomaly_Task");
    result.put("jobId", 1L);
    result.put("taskType", "MONITOR");
    result.put("id", 1L);
    result.put("taskInfo", "clob2: '{\"jobExecutionId\":1,\"monitorType\":\"UPDATE\",\"expireDaysAgo\":0}'");
    result.put("taskType", "MONITOR");
    result.put("lastModified", "2016-08-24 17:25:53.258");
    result.put("status", "RUNNING");
    result.put("lastModified", "2016-08-24 17:25:53.258");
    TaskDTO taskSpec1 = mapper.map(result, TaskDTO.class);
    System.out.println(taskSpec1);
    //INPUT 2
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("/tmp/map.out.1472093046128")));
    Map<String, Object> inputMap = (Map<String, Object>) ois.readObject();
    TaskDTO taskSpec2 = mapper.map(inputMap, TaskDTO.class);
    System.out.println(taskSpec2);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TaskDTO(com.linkedin.thirdeye.datalayer.dto.TaskDTO) File(java.io.File) BiMap(com.google.common.collect.BiMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) FileInputStream(java.io.FileInputStream) ModelMapper(org.modelmapper.ModelMapper) ObjectInputStream(java.io.ObjectInputStream)

Example 7 with ObjectInputStream

use of java.io.ObjectInputStream in project hibernate-orm by hibernate.

the class CacheKeySerializationTest method testId.

private void testId(CacheKeysFactory cacheKeysFactory, String entityName, Object id) throws Exception {
    final SessionFactoryImplementor sessionFactory = getSessionFactory(cacheKeysFactory.getClass().getName());
    final EntityPersister persister = sessionFactory.getEntityPersister(entityName);
    final Object key = cacheKeysFactory.createEntityKey(id, persister, sessionFactory, null);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(key);
    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final Object keyClone = ois.readObject();
    try {
        assertEquals(key, keyClone);
        assertEquals(keyClone, key);
        assertEquals(key.hashCode(), keyClone.hashCode());
        final Object idClone = cacheKeysFactory.getEntityId(keyClone);
        assertEquals(id.hashCode(), idClone.hashCode());
        assertEquals(id, idClone);
        assertEquals(idClone, id);
        assertTrue(persister.getIdentifierType().isEqual(id, idClone, sessionFactory));
        assertTrue(persister.getIdentifierType().isEqual(idClone, id, sessionFactory));
        sessionFactory.close();
    } finally {
        sessionFactory.close();
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ByteArrayInputStream(java.io.ByteArrayInputStream) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 8 with ObjectInputStream

use of java.io.ObjectInputStream in project Smack by igniterealtime.

the class JivePropertiesExtensionProvider method parse.

/**
     * Parse a properties sub-packet. If any errors occur while de-serializing Java object
     * properties, an exception will be printed and not thrown since a thrown exception will shut
     * down the entire connection. ClassCastExceptions will occur when both the sender and receiver
     * of the stanza(/packet) don't have identical versions of the same class.
     * <p>
     * Note that you have to explicitly enabled Java object deserialization with @{link
     * {@link JivePropertiesManager#setJavaObjectEnabled(boolean)}
     * 
     * @param parser the XML parser, positioned at the start of a properties sub-packet.
     * @return a map of the properties.
     * @throws IOException 
     * @throws XmlPullParserException 
     */
@Override
public JivePropertiesExtension parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
    Map<String, Object> properties = new HashMap<String, Object>();
    while (true) {
        int eventType = parser.next();
        if (eventType == XmlPullParser.START_TAG && parser.getName().equals("property")) {
            // Parse a property
            boolean done = false;
            String name = null;
            String type = null;
            String valueText = null;
            Object value = null;
            while (!done) {
                eventType = parser.next();
                if (eventType == XmlPullParser.START_TAG) {
                    String elementName = parser.getName();
                    if (elementName.equals("name")) {
                        name = parser.nextText();
                    } else if (elementName.equals("value")) {
                        type = parser.getAttributeValue("", "type");
                        valueText = parser.nextText();
                    }
                } else if (eventType == XmlPullParser.END_TAG) {
                    if (parser.getName().equals("property")) {
                        if ("integer".equals(type)) {
                            value = Integer.valueOf(valueText);
                        } else if ("long".equals(type)) {
                            value = Long.valueOf(valueText);
                        } else if ("float".equals(type)) {
                            value = Float.valueOf(valueText);
                        } else if ("double".equals(type)) {
                            value = Double.valueOf(valueText);
                        } else if ("boolean".equals(type)) {
                            value = Boolean.valueOf(valueText);
                        } else if ("string".equals(type)) {
                            value = valueText;
                        } else if ("java-object".equals(type)) {
                            if (JivePropertiesManager.isJavaObjectEnabled()) {
                                try {
                                    byte[] bytes = Base64.decode(valueText);
                                    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
                                    value = in.readObject();
                                } catch (Exception e) {
                                    LOGGER.log(Level.SEVERE, "Error parsing java object", e);
                                }
                            } else {
                                LOGGER.severe("JavaObject is not enabled. Enable with JivePropertiesManager.setJavaObjectEnabled(true)");
                            }
                        }
                        if (name != null && value != null) {
                            properties.put(name, value);
                        }
                        done = true;
                    }
                }
            }
        } else if (eventType == XmlPullParser.END_TAG) {
            if (parser.getName().equals(JivePropertiesExtension.ELEMENT)) {
                break;
            }
        }
    }
    return new JivePropertiesExtension(properties);
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) JivePropertiesExtension(org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension) ObjectInputStream(java.io.ObjectInputStream)

Example 9 with ObjectInputStream

use of java.io.ObjectInputStream in project java-design-patterns by iluwatar.

the class RainbowFishSerializer method readV1.

/**
   * Read V1 RainbowFish from file
   */
public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException {
    FileInputStream fileIn = new FileInputStream(filename);
    ObjectInputStream objIn = new ObjectInputStream(fileIn);
    Map<String, String> map = (Map<String, String>) objIn.readObject();
    objIn.close();
    fileIn.close();
    return new RainbowFish(map.get("name"), Integer.parseInt(map.get("age")), Integer.parseInt(map.get("lengthMeters")), Integer.parseInt(map.get("weightTons")));
}
Also used : Map(java.util.Map) HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 10 with ObjectInputStream

use of java.io.ObjectInputStream in project mapdb by jankotek.

the class JSR166Test method serialClone.

@SuppressWarnings("unchecked")
<T> T serialClone(T o) {
    try {
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serialBytes(o)));
        T clone = (T) ois.readObject();
        return clone;
    } catch (Throwable fail) {
        threadUnexpectedException(fail);
        return null;
    }
}
Also used : TT(org.mapdb.TT) ByteArrayInputStream(java.io.ByteArrayInputStream) 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