001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.serializer;
014
015import java.io.*;
016
017/**
018 * Subclass of {@link SerializerSession} for stream-based serializers.
019 * 
020 * <h5 class='topic'>Description</h5>
021 * 
022 * This class is the parent class of all byte-based serializers.
023 * <br>It has 1 abstract method to implement...
024 * <ul>
025 *    <li>{@link #doSerialize(SerializerPipe, Object)}
026 * </ul>
027 */
028public abstract class OutputStreamSerializerSession extends SerializerSession {
029
030   /**
031    * Create a new session using properties specified in the context.
032    * 
033    * @param ctx
034    *    The context creating this session object.
035    *    The context contains all the configuration settings for this object.
036    * @param args
037    *    Runtime arguments.
038    *    These specify session-level information such as locale and URI context.
039    *    It also include session-level properties that override the properties defined on the bean and
040    *    serializer contexts.
041    */
042   protected OutputStreamSerializerSession(Serializer ctx, SerializerSessionArgs args) {
043      super(ctx, args);
044   }
045
046   /**
047    * Constructor for sessions that don't require context.
048    * 
049    * @param args
050    *    Runtime session arguments.
051    */
052   protected OutputStreamSerializerSession(SerializerSessionArgs args) {
053      super(args);
054   }
055
056   @Override /* SerializerSession */
057   public final boolean isWriterSerializer() {
058      return false;
059   }
060
061   /**
062    * Convenience method for serializing an object to a <code><jk>byte</jk></code>.
063    * 
064    * @param o The object to serialize.
065    * @return The output serialized to a byte array.
066    * @throws SerializeException If a problem occurred trying to convert the output.
067    */
068   @Override /* SerializerSession */
069   public final byte[] serialize(Object o) throws SerializeException {
070      ByteArrayOutputStream baos = new ByteArrayOutputStream();
071      serialize(o, baos);
072      return baos.toByteArray();
073   }
074}