Search in sources :

Example 36 with XMLDecoder

use of java.beans.XMLDecoder in project jdk8u_jdk by JetBrains.

the class Test6329581 method decode.

private Object decode(byte[] array) {
    ByteArrayInputStream in = new ByteArrayInputStream(array);
    XMLDecoder decoder = new XMLDecoder(in, null, this, this);
    Object object = decoder.readObject();
    validate(object);
    decoder.close();
    return object;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder)

Example 37 with XMLDecoder

use of java.beans.XMLDecoder in project aima-java by aimacode.

the class MapViewPopup method actionPerformed.

@Override
public void actionPerformed(ActionEvent ae) {
    if (ae.getSource() == entityInfoMenuItem) {
        MapNode mNode = pane.getRenderer().getNextNode(x, y);
        if (mNode != null)
            pane.showMapEntityInfoDialog(mNode, pane.isDebugModeEnabled());
    } else if (ae.getSource() == clearMenuItem) {
        pane.getMap().clearMarkersAndTracks();
    } else if (ae.getSource() == createMarkerMenuItem) {
        PositionPanel panel = new PositionPanel();
        int res = JOptionPane.showConfirmDialog(pane, panel, "Specify a Position", JOptionPane.OK_CANCEL_OPTION);
        if (res == JOptionPane.OK_OPTION) {
            float lat = panel.getLat();
            float lon = panel.getLon();
            if (!Float.isNaN(lat) && !Float.isNaN(lon)) {
                pane.getMap().addMarker(lat, lon);
                pane.adjustToCenter(lat, lon);
            }
        }
    } else if (ae.getSource() == removeMarkerMenuItem) {
        pane.removeNearestMarker(x, y);
    } else if (ae.getSource() == loadMarkersMenuItem) {
        XMLDecoder decoder = null;
        try {
            File xmlFile = null;
            if (getFileChooser().showDialog(pane, "Load Markers") == JFileChooser.APPROVE_OPTION) {
                xmlFile = getFileChooser().getSelectedFile();
                if (!xmlFile.getPath().contains("."))
                    xmlFile = new File(xmlFile.getPath() + ".xml");
            }
            if (xmlFile != null && xmlFile.exists()) {
                pane.getMap().clearMarkersAndTracks();
                decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(xmlFile)));
                int size = (Integer) decoder.readObject();
                for (int i = 0; i < size; i++) {
                    WritablePosition pos = (WritablePosition) decoder.readObject();
                    pane.getMap().addMarker(pos.getLat(), pos.getLon());
                }
                pane.fireMapViewEvent(new MapViewEvent(pane, MapViewEvent.Type.MARKER_ADDED));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (decoder != null)
                decoder.close();
        }
    } else if (ae.getSource() == saveMarkersMenuItem) {
        XMLEncoder encoder = null;
        try {
            File xmlFile = null;
            if (getFileChooser().showDialog(pane, "Save Markers") == JFileChooser.APPROVE_OPTION) {
                xmlFile = getFileChooser().getSelectedFile();
                if (!xmlFile.getPath().contains("."))
                    xmlFile = new File(xmlFile.getPath() + ".xml");
                encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(xmlFile)));
                encoder.writeObject(pane.getMap().getMarkers().size());
                for (MapNode node : pane.getMap().getMarkers()) encoder.writeObject(new WritablePosition(node));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (encoder != null)
                encoder.close();
        }
    } else if (ae.getSource() == functionsMenuItem) {
        JOptionPane.showMessageDialog(pane, MapViewPane.FUNCTION_DESCRIPTION.split("\\|"), "Function Description", JOptionPane.INFORMATION_MESSAGE);
    } else if (ae.getSource() == debugMenuItem) {
        pane.enableDebugMode(debugMenuItem.isSelected());
    }
}
Also used : MapViewEvent(aimax.osm.viewer.MapViewEvent) MapNode(aimax.osm.data.entities.MapNode) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) XMLEncoder(java.beans.XMLEncoder) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) XMLDecoder(java.beans.XMLDecoder) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 38 with XMLDecoder

use of java.beans.XMLDecoder in project jdk8u_jdk by JetBrains.

the class JavaElementHandler method getValue.

/**
     * Returns the owner of the owner document handler
     * as a value of &lt;java&gt; element.
     *
     * @return the owner of the owner document handler
     */
private Object getValue() {
    Object owner = getOwner().getOwner();
    if ((this.type == null) || isValid(owner)) {
        return owner;
    }
    if (owner instanceof XMLDecoder) {
        XMLDecoder decoder = (XMLDecoder) owner;
        owner = decoder.getOwner();
        if (isValid(owner)) {
            return owner;
        }
    }
    throw new IllegalStateException("Unexpected owner class: " + owner.getClass().getName());
}
Also used : XMLDecoder(java.beans.XMLDecoder)

Example 39 with XMLDecoder

use of java.beans.XMLDecoder in project OpenGrok by OpenGrok.

the class Message method decodeObject.

private static Message decodeObject(InputStream in) throws IOException {
    final Object ret;
    final LinkedList<Exception> exceptions = new LinkedList<>();
    try (XMLDecoder d = new XMLDecoder(new BufferedInputStream(in))) {
        ret = d.readObject();
    }
    if (!(ret instanceof Message)) {
        throw new IOException("Not a valid message file");
    }
    Message conf = ((Message) ret);
    return conf;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) XMLDecoder(java.beans.XMLDecoder) IOException(java.io.IOException) IOException(java.io.IOException) LinkedList(java.util.LinkedList)

Example 40 with XMLDecoder

use of java.beans.XMLDecoder in project OpenGrok by OpenGrok.

the class ProjectTest method testEncodeDecode.

/**
 * Test that a {@code Project} instance can be encoded and decoded without
 * errors. Bug #3077.
 */
@Test
public void testEncodeDecode() {
    // Create an exception listener to detect errors while encoding and
    // decoding
    final LinkedList<Exception> exceptions = new LinkedList<Exception>();
    ExceptionListener listener = new ExceptionListener() {

        public void exceptionThrown(Exception e) {
            exceptions.addLast(e);
        }
    };
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLEncoder enc = new XMLEncoder(out);
    enc.setExceptionListener(listener);
    Project p1 = new Project("foo");
    enc.writeObject(p1);
    enc.close();
    // verify that the write didn't fail
    if (!exceptions.isEmpty()) {
        AssertionFailedError afe = new AssertionFailedError("Got " + exceptions.size() + " exception(s)");
        // Can only chain one of the exceptions. Take the first one.
        afe.initCause(exceptions.getFirst());
        throw afe;
    }
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    XMLDecoder dec = new XMLDecoder(in, null, listener);
    Project p2 = (Project) dec.readObject();
    assertNotNull(p2);
    dec.close();
    // verify that the read didn't fail
    if (!exceptions.isEmpty()) {
        AssertionFailedError afe = new AssertionFailedError("Got " + exceptions.size() + " exception(s)");
        // Can only chain one of the exceptions. Take the first one.
        afe.initCause(exceptions.getFirst());
        throw afe;
    }
}
Also used : XMLEncoder(java.beans.XMLEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AssertionFailedError(junit.framework.AssertionFailedError) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

XMLDecoder (java.beans.XMLDecoder)54 ByteArrayInputStream (java.io.ByteArrayInputStream)28 IOException (java.io.IOException)19 BufferedInputStream (java.io.BufferedInputStream)18 XMLEncoder (java.beans.XMLEncoder)15 FileInputStream (java.io.FileInputStream)14 LinkedList (java.util.LinkedList)13 ExceptionListener (java.beans.ExceptionListener)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 File (java.io.File)8 FileOutputStream (java.io.FileOutputStream)5 InputStream (java.io.InputStream)5 AssertionFailedError (junit.framework.AssertionFailedError)5 Test (org.junit.Test)5 Test (org.junit.jupiter.api.Test)5 FileNotFoundException (java.io.FileNotFoundException)4 List (java.util.List)4 PatternSyntaxException (java.util.regex.PatternSyntaxException)4 BufferedOutputStream (java.io.BufferedOutputStream)3 Serializable (java.io.Serializable)2