All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.juneau.serializer.SerializerPipe Maven / Gradle / Ivy

There is a newer version: 9.0.1
Show newest version
// ***************************************************************************************************************************
// * 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.juneau.serializer;

import static org.apache.juneau.internal.IOUtils.*;

import java.io.*;

import org.apache.juneau.*;
import org.apache.juneau.internal.*;

/**
 * A wrapper around an object that a serializer sends its output to.
 *
 * 

* For character-based serializers, the output object can be any of the following: *

    *
  • {@link Writer} *
  • {@link OutputStream} - Output will be written as UTF-8 encoded stream. *
  • {@link File} - Output will be written as system-default encoded stream. *
  • {@link StringBuilder} *
* *

* For stream-based serializers, the output object can be any of the following: *

    *
  • {@link OutputStream} *
  • {@link File} *
*/ public final class SerializerPipe { private final Object output; private final boolean autoClose; private OutputStream outputStream; private Writer writer; /** * Constructor. * * @param output The object to pipe the serializer output to. */ SerializerPipe(Object output) { this.output = output; this.autoClose = (output instanceof File); } /** * Wraps the specified output object inside an output stream. * *

* Subclasses can override this method to implement their own specialized output streams. * *

* This method can be used if the output object is any of the following class types: *

    *
  • {@link OutputStream} *
  • {@link File} *
* * @return The output object wrapped in an output stream. * @throws IOException If object could not be converted to an output stream. */ public OutputStream getOutputStream() throws IOException { if (output == null) throw new IOException("Output cannot be null."); if (output instanceof OutputStream) outputStream = (OutputStream)output; else if (output instanceof File) outputStream = new BufferedOutputStream(new FileOutputStream((File)output)); else throw new IOException("Cannot convert object of type "+output.getClass().getName()+" to an OutputStream."); return outputStream; } /** * Wraps the specified output object inside a writer. * *

* Subclasses can override this method to implement their own specialized writers. * *

* This method can be used if the output object is any of the following class types: *

    *
  • {@link Writer} *
  • {@link OutputStream} - Output will be written as UTF-8 encoded stream. *
  • {@link File} - Output will be written as system-default encoded stream. *
* * @return The output object wrapped in a Writer. * @throws IOException If object could not be converted to a writer. */ public Writer getWriter() throws IOException { if (output == null) throw new IOException("Output cannot be null."); if (output instanceof Writer) writer = (Writer)output; else if (output instanceof OutputStream) writer = new OutputStreamWriter((OutputStream)output, UTF8); else if (output instanceof File) writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream((File)output))); else if (output instanceof StringBuilder) writer = new StringBuilderWriter((StringBuilder)output); else throw new IOException("Cannot convert object of type "+output.getClass().getName()+" to a Writer."); return writer; } /** * Overwrites the writer in this pipe. * *

* Used when wrapping the writer returned by {@link #getWriter()} so that the wrapped writer will be flushed * and closed when {@link #close()} is called. * * @param writer The wrapped writer. */ public void setWriter(Writer writer) { this.writer = writer; } /** * Overwrites the output stream in this pipe. * *

* Used when wrapping the stream returned by {@link #getOutputStream()} so that the wrapped stream will be flushed * and closed when {@link #close()} is called. * * @param outputStream The wrapped stream. */ public void setOutputStream(OutputStream outputStream) { this.outputStream = outputStream; } /** * Returns the raw output object passed into this session. * * @return The raw output object passed into this session. */ public Object getRawOutput() { return output; } /** * Closes the output pipe. */ public void close() { try { IOUtils.flush(writer, outputStream); if (autoClose) IOUtils.close(writer, outputStream); } catch (IOException e) { throw new BeanRuntimeException(e); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy