static public <S extends Serializable> S clone(S object) throws ClassNotFoundException, IOException { return clone(object, 1024 * 1024); }
static public PMML deserializePMML(InputStream is) throws ClassNotFoundException, IOException { return (PMML)deserialize(is); }
static public void serializePMML(PMML pmml, OutputStream os) throws IOException { serialize(pmml, os); }
@Test public void compressedSequence() throws Exception { PMML pmml = ResourceUtil.unmarshal(Version.PMML_4_3); pmml.accept(new LocatorNullifier()); DirectByteArrayOutputStream buffer = new DirectByteArrayOutputStream(3 * 1024); try(OutputStream os = new GZIPOutputStream(buffer)){ SerializationUtil.serializePMML(pmml, os); SerializationUtil.serializePMML(pmml, os); os.flush(); } try(InputStream is = new GZIPInputStream(buffer.getInputStream())){ PMML firstPmml = SerializationUtil.deserializePMML(is); PMML secondPmml = SerializationUtil.deserializePMML(is); assertNotSame(firstPmml, secondPmml); assertEquals(-1, is.read()); } } }
@SuppressWarnings ( value = {"unchecked"} ) static public <S extends Serializable> S clone(S object, int capacity) throws ClassNotFoundException, IOException { DirectByteArrayOutputStream buffer = new DirectByteArrayOutputStream(capacity); serialize(object, buffer); try(InputStream is = buffer.getInputStream()){ return (S)deserialize(is); } } }
private void transform(File pmmlFile, File serFile, VisitorBattery visitorBattery) throws Exception { PMML pmml; try(InputStream is = new FileInputStream(pmmlFile)){ pmml = PMMLUtil.unmarshal(is); } visitorBattery.applyTo(pmml); try(OutputStream os = new FileOutputStream(serFile)){ SerializationUtil.serializePMML(pmml, os); } }
@SuppressWarnings ( value = {"unchecked"} ) static public <S extends Serializable> S clone(S object, int capacity) throws ClassNotFoundException, IOException { DirectByteArrayOutputStream buffer = new DirectByteArrayOutputStream(capacity); serialize(object, buffer); try(InputStream is = buffer.getInputStream()){ return (S)deserialize(is); } } }
static public <S extends Serializable> S clone(S object) throws ClassNotFoundException, IOException { return clone(object, 1024 * 1024); }
static public PMML deserializePMML(InputStream is) throws ClassNotFoundException, IOException { return (PMML)deserialize(is); }
static public void serializePMML(PMML pmml, OutputStream os) throws IOException { serialize(pmml, os); }
@Test public void mixedContent() throws Exception { PMML pmml = ResourceUtil.unmarshal(getClass()); List<?> content = ExtensionUtil.getContent(pmml); assertEquals(5, content.size()); assertEquals("First text value", content.get(0)); assertEquals(Arrays.asList("First extension"), getDeepContent(content.get(1))); assertEquals("Second text value", content.get(2)); assertEquals(Arrays.asList("Second extension"), getDeepContent(content.get(3))); assertEquals("Third text value", content.get(4)); try { SerializationUtil.clone(pmml); fail(); } catch(NotSerializableException nse){ // Ignored } pmml.accept(new LocatorTransformer()); SerializationUtil.clone(pmml); }
@Test public void transformAndClone() throws Exception { PMML pmml = ResourceUtil.unmarshal(Version.PMML_4_3); assertNotNull(pmml.getLocator()); try { SerializationUtil.clone(pmml); fail(); } catch(NotSerializableException nse){ // Ignored } pmml.accept(new LocatorTransformer()); assertNotNull(pmml.getLocator()); SerializationUtil.clone(pmml); }
@Test public void nullifyAndClone() throws Exception { PMML pmml = ResourceUtil.unmarshal(Version.PMML_4_3); assertNotNull(pmml.getLocator()); try { SerializationUtil.clone(pmml); fail(); } catch(NotSerializableException nse){ // Ignored } pmml.accept(new LocatorNullifier()); assertNull(pmml.getLocator()); SerializationUtil.clone(pmml); }
@Test public void readResolve() throws Exception { FieldName name = FieldName.create("x"); assertTrue(name.isInterned()); FieldName clonedName = SerializationUtil.clone(name); assertTrue(clonedName.isInterned()); assertSame(name, clonedName); name = new FieldName("x"); assertFalse(name.isInterned()); clonedName = SerializationUtil.clone(name); assertTrue(clonedName.isInterned()); assertNotSame(name, clonedName); } }