<commit_before>//
// typica - A client library for Amazon Web Services
// Copyright (C) 2007 Xerox Corporation
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package com.xerox.amazonws.common;

import java.io.InputStream;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 * This class implements some helpful methods to marshal and unmarshal xml.
 *
 * @author <NAME>
 * @author <EMAIL>
 */
public class JAXBuddy {
    public static <T> InputStream serializeXMLFile(Class<T> c, Object object) 
            throws JAXBException, IOException {
        Marshaller m = getMarshaller(c);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        m.marshal(object, baos);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        return bais;
    }

    public static <T> String serializeXMLString(Class<T> c, Object object) 
            throws JAXBException, IOException {
        Marshaller m = getMarshaller(c);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        m.marshal(object, baos);
		return new String(baos.toByteArray());
    }
 
    public static <T> T deserializeXMLStream(Class<T> c, InputStream is) throws JAXBException {
        Unmarshaller u = getUnmarshaller(c);
        T result = c.cast(u.unmarshal(is));
        return result;
    }
    
    private static Marshaller getMarshaller(Class<?> c) throws JAXBException {
        String typePackage = c.getPackage().getName();
        JAXBContext jc = JAXBContext.newInstance(typePackage);
        Marshaller m = jc.createMarshaller();
        return m;
    }

    private static Unmarshaller getUnmarshaller(Class<?> c) throws JAXBException {
        String typePackage = c.getPackage().getName();
        JAXBContext jc = JAXBContext.newInstance(typePackage, c.getClassLoader());
        Unmarshaller u = jc.createUnmarshaller();
        return u;
    }
}<commit_msg>added caching of marshaller and unmarshaller objects. good speedup on all WS calls\!<commit_after>//
// typica - A client library for Amazon Web Services
// Copyright (C) 2007 Xerox Corporation
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package com.xerox.amazonws.common;

import java.io.InputStream;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Hashtable;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 * This class implements some helpful methods to marshal and unmarshal xml.
 *
 * @author <NAME>
 * @author <EMAIL>
 */
public class JAXBuddy {
	public static Hashtable<Class, Marshaller> marshallerCache = new Hashtable<Class, Marshaller>();
	public static Hashtable<Class, Unmarshaller> unmarshallerCache = new Hashtable<Class, Unmarshaller>();

    public static <T> InputStream serializeXMLFile(Class<T> c, Object object) 
            throws JAXBException, IOException {
        Marshaller m = getMarshaller(c);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        m.marshal(object, baos);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        return bais;
    }

    public static <T> String serializeXMLString(Class<T> c, Object object) 
            throws JAXBException, IOException {
        Marshaller m = getMarshaller(c);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        m.marshal(object, baos);
		return new String(baos.toByteArray());
    }
 
    public static <T> T deserializeXMLStream(Class<T> c, InputStream is) throws JAXBException {
        Unmarshaller u = getUnmarshaller(c);
        T result = c.cast(u.unmarshal(is));
        return result;
    }
    
    private static Marshaller getMarshaller(Class<?> c) throws JAXBException {
		Marshaller m = marshallerCache.get(c);
		if (m == null) {
        	String typePackage = c.getPackage().getName();
        	JAXBContext jc = JAXBContext.newInstance(typePackage);
        	m = jc.createMarshaller();
			marshallerCache.put(c, m);
		}
        return m;
    }

    private static Unmarshaller getUnmarshaller(Class<?> c) throws JAXBException {
		Unmarshaller u = unmarshallerCache.get(c);
		if (u == null) {
			String typePackage = c.getPackage().getName();
			JAXBContext jc = JAXBContext.newInstance(typePackage, c.getClassLoader());
			u = jc.createUnmarshaller();
			unmarshallerCache.put(c, u);
		}
        return u;
    }
}
{'fraction_non_alphanumeric': 0.09796348314606741, 'fraction_numerical': 0.0028089887640449437, 'mean_word_length': 3.4300155520995332, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3510640', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1650, 'n_words': 508}