/*******************************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.wink.common.internal.providers.entity.xml;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.transform.stream.StreamSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.wink.common.utils.ProviderUtils;

@Provider
@Consumes( {MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.WILDCARD})
@Produces( {MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.WILDCARD})
public class JAXBXmlProvider extends AbstractJAXBProvider implements MessageBodyReader<Object>,
    MessageBodyWriter<Object> {

    private static final Logger logger = LoggerFactory.getLogger(JAXBXmlProvider.class);

    public boolean isReadable(Class<?> type,
                              Type genericType,
                              Annotation[] annotations,
                              MediaType mediaType) {
        return isJAXBObject(type, genericType) && isSupportedMediaType(mediaType);
    }

    public Object readFrom(Class<Object> type,
                           Type genericType,
                           Annotation[] annotations,
                           MediaType mediaType,
                           MultivaluedMap<String, String> httpHeaders,
                           InputStream entityStream) throws IOException, WebApplicationException {

        Unmarshaller unmarshaller = null;
        Object unmarshaledResource = null;
        try {
            unmarshaller = getUnmarshaller(type, mediaType);
            if (type.isAnnotationPresent(XmlRootElement.class))
                unmarshaledResource = unmarshaller.unmarshal(entityStream);
            else
                unmarshaledResource =
                    unmarshaller.unmarshal(new StreamSource(entityStream), type).getValue();

        } catch (JAXBException e) {
            logger.error("Failed to unmarshal {}", type.getName());
            throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
        }
        return unmarshaledResource;
    }

    public long getSize(Object t,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType) {
        return -1;
    }

    public boolean isWriteable(Class<?> type,
                               Type genericType,
                               Annotation[] annotations,
                               MediaType mediaType) {
        return isJAXBObject(type, genericType) && isSupportedMediaType(mediaType);
    }

    public void writeTo(Object t,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream) throws IOException, WebApplicationException {
        try {
            Marshaller marshaller = getMarshaller(type, mediaType);
            OutputStreamWriter writer =
                new OutputStreamWriter(entityStream, ProviderUtils.getCharset(mediaType));
            Object entityToMarshal = getEntityToMarshal(t, type);
            marshaller.marshal(entityToMarshal, writer);
            writer.flush();
        } catch (JAXBException e) {
            logger.error("Failed to marshal {}", type.getName());
            throw new WebApplicationException(e);
        }
    }

}

@@ -1124,43 +1124,8 @@
 am;%0A
-import java.io.OutputStreamWriter;%0A
 impo
@@ -1736,28 +1736,54 @@
 ort org.
-slf4j.Logger
+apache.wink.common.utils.ProviderUtils
 ;%0Aimport
@@ -1799,23 +1799,16 @@
 j.Logger
-Factory
 ;%0Aimport
@@ -1816,46 +1816,27 @@
 org.
-apache.wink.common.utils.ProviderUtils
+slf4j.LoggerFactory
 ;%0A%0A@
@@ -4547,32 +4547,33 @@
 pe, mediaType);%0A
+%0A
             Outp
@@ -4572,87 +4572,55 @@
     
-OutputStreamWriter writer =%0A                new OutputStreamWriter(entityStream
+marshaller.setProperty(Marshaller.JAXB_ENCODING
 , Pr
@@ -4651,24 +4651,25 @@
 ediaType));%0A
+%0A
             
@@ -4717,24 +4717,106 @@
 al(t, type);
+%0A%0A            // Use an OutputStream directly instead of a Writer for performance.
 %0A           
@@ -4852,22 +4852,28 @@
 arshal, 
-writer
+entityStream
 );%0A     
@@ -4883,14 +4883,20 @@
     
-writer
+entityStream
 .flu

Use OutputStream instead of Writer for performance