Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import java.io.OutputStream;

import java.io.StringWriter;

public class Main {
    private static JAXBContext jaxbContext;
    private static ThreadLocal<Marshaller> marshaller = new ThreadLocal<Marshaller>();

    public static String marshal(Object bean) throws JAXBException {
        StringWriter writer = new StringWriter();
        getMarshaller().marshal(bean, writer);
        return writer.toString();
    }

    public static void marshal(Object bean, OutputStream os) throws JAXBException {
        getMarshaller().marshal(bean, os);
    }

    private static Marshaller getMarshaller() throws JAXBException {
        Marshaller m = marshaller.get();
        if (m == null) {
            m = jaxbContext.createMarshaller();
            marshaller.set(m);
        }
        return m;
    }
}