Example usage for java.beans XMLEncoder close

List of usage examples for java.beans XMLEncoder close

Introduction

In this page you can find the example usage for java.beans XMLEncoder close.

Prototype

public void close() 

Source Link

Document

This method calls flush , writes the closing postamble and then closes the output stream associated with this stream.

Usage

From source file:net.sourceforge.fenixedu.util.tests.ResponseExternalization.java

public static String externalize(Response source) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(out);
    encoder.writeObject(source);//from   w ww  . j a va  2  s  . c  o m
    encoder.close();
    try {
        // I think that this is wrong and that we should get the
        // bytes of the ByteArrayOutputStream interpreted as
        // UTF-8, which is what the XMLEncoder produces in the
        // first place.
        // WARNING: If this is changed, the internalize method
        // should be changed accordingly.
        return out.toString(Charset.defaultCharset().name());
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        logger.error(e.getMessage(), e);
        return out.toString();
    }
}

From source file:Main.java

/***
 * Serialize an object into xml string./* ww  w  .j  a  va2  s. co m*/
 * 
 * @param obj
 *            object to serialize
 * @return Xml string represents the object
 */
public static String serializeObject(final Object obj) {
    final OutputStream bufferStream = new ByteArrayOutputStream();

    // Create XML encoder.
    final XMLEncoder xenc = new XMLEncoder(bufferStream);

    xenc.writeObject(obj);
    // Need to close the XMLEncoder to flush.
    xenc.close();

    final String serializedString = bufferStream.toString();

    // JavaDoc indicates that we will not need to close
    // a ByteArrayOutputStream. From JavaDoc:
    // Closing a ByteArrayOutputStream has no effect. The methods
    // in this class can be called after the stream has been closed
    // without generating an IOException.
    //
    // bufferStream.close();

    return serializedString;
}

From source file:fi.vm.sade.log.client.LoggerJms.java

/**
 * Encode messages to string (xml)./*from  w w w .  j  av a 2 s .  co  m*/
 *
 * @param event
 * @return
 */
public static String encode(LogEvent event) {
    if (event == null) {
        return null;
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEncoder xmlEncoder = new XMLEncoder(baos);
    xmlEncoder.writeObject(event);
    xmlEncoder.close();

    return baos.toString();
}

From source file:Main.java

/**
 * Serializes an object into an xml file
 * /*w  w w . ja  v  a  2s. c  o m*/
 * @param object object to serialize
 * @param fileName path to file
 */
public static void encodeToFile(Object object, String fileName) throws FileNotFoundException, IOException {
    XMLEncoder encoder = new XMLEncoder(new FileOutputStream(fileName));
    try {
        encoder.writeObject(object);
        encoder.flush();
    } finally {
        encoder.close();
    }
}

From source file:Main.java

/**
 * Creates from the given Object an xml string.
 * //from   ww  w  .j a  va2s . com
 * @param <T>
 *            the generic type of the return type
 * @param obj
 *            the obj to transform to an xml string.
 * @return the xml string
 */
public static <T> String toXmlWithXMLEncoder(final T obj) {
    XMLEncoder enc = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        enc = new XMLEncoder(baos);
        enc.writeObject(obj);
        enc.close();
        enc = null;
    } finally {
        if (enc != null) {
            enc.close();
        }
    }
    return baos.toString();
}

From source file:Main.java

public static void writeBeanToXml(String path, Object bean) {
    XMLEncoder encoder = null;
    try {/*  www . ja v a2  s .c  o m*/
        // Serialize object into XML
        encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(path)));
        encoder.writeObject(bean);
        encoder.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (encoder != null) {
            encoder.close();
        }
    }
}

From source file:Main.java

/**
 * Creates from the given Object an xml string.
 * //from ww  w .  ja  v a 2  s.  c  om
 * @param <T>
 *            the generic type of the return type
 * @param obj
 *            the obj to transform to an xml string.
 * @return the xml string
 */
public static <T> String toXmlWithXMLEncoder(final T obj) {
    XMLEncoder enc = null;
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        enc = new XMLEncoder(baos);
        enc.writeObject(obj);
        enc.close();
        enc = null;
    } finally {
        if (enc != null) {
            enc.close();
        }
    }
    return baos.toString();
}

From source file:com.npower.dl.FirmwareMimeTypeHelper.java

public static void save(File file) throws IOException {
    FileOutputStream out = new FileOutputStream(file);
    XMLEncoder xmlEncoder = new XMLEncoder(out);
    xmlEncoder.writeObject(mimeTypes);/*from  ww w  . j a va2s .co m*/
    xmlEncoder.flush();
    xmlEncoder.close();
    out.close();
}

From source file:Main.java

/**
 * Writes an object to an XML file.//  w  ww.  j av  a2  s .  c om
 * 
 * @param bean the bean
 * @param filename the filename
 * 
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void writeBeanToFile(Object bean, String filename) throws IOException {
    XMLEncoder encoder = null;
    try {
        encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(filename)));
        encoder.writeObject(bean);
    } finally {
        if (encoder != null) {
            encoder.close();
        }
    }
}

From source file:Main.java

public static byte[] encodeObject(Object object, final boolean noisy) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(outputStream);
    encoder.setExceptionListener(new ExceptionListener() {
        public void exceptionThrown(Exception ex) {
            if (noisy)
                ex.printStackTrace();/*from ww w. jav  a2s. c om*/
        }
    });

    encoder.writeObject(object);
    encoder.close();

    if (noisy)
        System.out.println(outputStream.toString());

    return outputStream.toByteArray();
}