public static Matrix fromStream(InputStream stream, Object... parameters) throws FileNotFoundException, IOException, ClassNotFoundException { return (Matrix) SerializationUtil.deserialize(stream); } }
@SuppressWarnings("unchecked") public V readValue(InputStream is) { try { return (V) SerializationUtil.deserialize(is); } catch (Exception e) { e.printStackTrace(); return null; } }
public static Object deserialize(byte[] data) throws ClassNotFoundException, IOException { ByteArrayInputStream bis = new ByteArrayInputStream(data); Object o = deserialize(bis); return o; }
@SuppressWarnings("unchecked") public V get(Object key) { try { byte[] data = map.get(key); return data == null ? null : (V) SerializationUtil.deserialize(data); } catch (Exception e) { throw new RuntimeException(e); } }
public static Object load(File file) throws ClassNotFoundException, IOException { FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); Object o = deserialize(bis); bis.close(); fis.close(); return o; }
public Classifier loadFromFile(File file) throws Exception { FileInputStream fi = new FileInputStream(file); BufferedInputStream bi = new BufferedInputStream(fi); Classifier c = (Classifier) SerializationUtil.deserialize(bi); bi.close(); fi.close(); return c; }
public static Object loadCompressed(File file) throws ClassNotFoundException, IOException { FileInputStream fis = new FileInputStream(file); GZIPInputStream bis = new GZIPInputStream(fis); Object o = deserialize(bis); bis.close(); fis.close(); return o; }
public CoreObject clone() { try { return (CoreObject) SerializationUtil.deserialize(SerializationUtil.serialize(this)); } catch (Exception e) { e.printStackTrace(); return null; } } }
@SuppressWarnings("unchecked") private K getKeyForFile(File file) throws ClassNotFoundException, IOException { String filename = file.getName(); if (filename.endsWith(".gz")) { filename = filename.substring(0, filename.length() - 3); } if (filename.endsWith(".dat")) { filename = filename.substring(0, filename.length() - 4); } if (filename.endsWith(".obj")) { filename = filename.substring(0, filename.length() - 4); filename = StringUtil.reverse(filename); return (K) SerializationUtil.deserialize(StringUtil.decodeFromHex(filename)); } else { return (K) filename; } }
public synchronized Sample getSampleAt(int index) throws Exception { Document doc = getIndexSearcher().doc(index); Sample s = (Sample) SerializationUtil.deserialize(doc.getBinaryValue("rawdata").bytes); return s; }
public V get(Object key) { try { prepareReader(); Text k = new Text(SerializationUtil.serialize((Serializable) key)); Text v = new Text(); Text t = (Text) reader.get(k, v); if (t == null || t.getBytes() == null || t.getBytes().length == 0) { return null; } return (V) SerializationUtil.deserialize(t.getBytes()); } catch (Exception e) { throw new RuntimeException("could not get value for key: " + key, e); } }
@SuppressWarnings("unchecked") public synchronized V get(Object key) { try { Term term = new Term(KEYSTRING, getUniqueString(key)); TermQuery query = new TermQuery(term); TopDocs docs = getIndexSearcher().search(query, 1); if (docs.totalHits > 0) { ScoreDoc match = docs.scoreDocs[0]; Document doc = getIndexSearcher().doc(match.doc); return (V) SerializationUtil.deserialize(doc.getBinaryValue(VALUEDATA).bytes); } } catch (Exception e) { throw new RuntimeException("could not search documents: " + key, e); } return null; }
@SuppressWarnings("unchecked") public synchronized V get(Object key) { try { Term term = new Term(KEYSTRING, getUniqueString(key)); TermQuery query = new TermQuery(term); TopDocs docs = getIndexSearcher().search(query, 1); if (docs.totalHits > 0) { ScoreDoc match = docs.scoreDocs[0]; Document doc = getIndexSearcher().doc(match.doc); return (V) SerializationUtil.deserialize(doc.getBinaryValue(VALUEDATA).bytes); } } catch (Exception e) { throw new RuntimeException("could not search documents: " + key, e); } return null; }
@SuppressWarnings("unchecked") public synchronized Set<K> keySet() { Set<K> set = new HashSet<K>(); if (isEmpty()) { return set; } try { Term term = new Term(KEYSTRING, "*"); WildcardQuery query = new WildcardQuery(term); TopDocs docs = getIndexSearcher().search(query, MAXSEARCHSIZE); for (ScoreDoc sd : docs.scoreDocs) { Document d = getIndexSearcher().doc(sd.doc); set.add((K) SerializationUtil.deserialize(d.getBinaryValue(KEYDATA).bytes)); } return set; } catch (Exception e) { throw new RuntimeException("could not search documents", e); } }
@SuppressWarnings("unchecked") public synchronized Set<K> keySet() { Set<K> set = new HashSet<K>(); if (isEmpty()) { return set; } try { Term term = new Term(KEYSTRING, "*"); WildcardQuery query = new WildcardQuery(term); TopDocs docs = getIndexSearcher().search(query, MAXSEARCHSIZE); for (ScoreDoc sd : docs.scoreDocs) { Document d = getIndexSearcher().doc(sd.doc); set.add((K) SerializationUtil.deserialize(d.getBinaryValue(KEYDATA).bytes)); } return set; } catch (Exception e) { throw new RuntimeException("could not search documents", e); } }
public synchronized ObjectMatrix2D search(String searchString) { try { MultiFieldQueryParser p = new MultiFieldQueryParser(Version.LUCENE_47, new String[] { VALUESTRING }, getAnalyzer()); Query query = p.parse(searchString); TopDocs docs = getIndexSearcher().search(query, 100); ObjectMatrix2D result = ObjectMatrix2D.Factory.zeros(docs.totalHits, 3); for (int row = 0; row < docs.totalHits; row++) { ScoreDoc match = docs.scoreDocs[row]; Document doc = getIndexSearcher().doc(match.doc); result.setAsFloat(match.score, row, 0); result.setAsObject(SerializationUtil.deserialize(doc.getBinaryValue(KEYDATA).bytes), row, 1); result.setAsObject(SerializationUtil.deserialize(doc.getBinaryValue(VALUEDATA).bytes), row, 2); } return result; } catch (Exception e) { throw new RuntimeException("could not search documents: " + searchString, e); } }
@SuppressWarnings("unchecked") @Test public void testSerialize() throws Exception { List<String> m = createList(); m.add("a"); m.add("b"); if (m instanceof Serializable) { byte[] data = SerializationUtil.serialize((Serializable) m); List<String> m2 = (List<String>) SerializationUtil.deserialize(data); assertEquals(getLabel(), m, m2); } }
@SuppressWarnings("unchecked") @Test public void testSerialize() throws Exception { Set<String> m = createSet(); m.add("a"); m.add("b"); if (m instanceof Serializable) { byte[] data = SerializationUtil.serialize((Serializable) m); Set<String> m2 = (Set<String>) SerializationUtil.deserialize(data); assertEquals(getLabel(), m, m2); } }
@SuppressWarnings("unchecked") @Test public void testSerialize() throws Exception { Map<String, String> m = createMap(); m.put("a", "test1"); m.put("b", "test2"); if (m instanceof Serializable) { byte[] data = SerializationUtil.serialize((Serializable) m); Map<String, String> m2 = (Map<String, String>) SerializationUtil.deserialize(data); assertEquals(getLabel(), m, m2); } if (m instanceof Erasable) { ((Erasable) m).erase(); } if (m instanceof Closeable) { ((Closeable) m).close(); } }
@Test public final void testSerialize() throws Exception { Matrix m = createMatrixWithAnnotation(2, 2); m.setAsDouble(1.0, 0, 0); m.setAsDouble(2.0, 0, 1); m.setAsDouble(3.0, 1, 0); m.setAsDouble(4.0, 1, 1); byte[] data = SerializationUtil.serialize(m); Matrix m2 = (Matrix) SerializationUtil.deserialize(data); if (m2.isTransient()) { Matrix m0 = Matrix.Factory.zeros(2, 2); assertEquals(getLabel(), m0, m2); } else { assertEquals(getLabel(), m, m2); } if (m instanceof Erasable) { ((Erasable) m).erase(); } }