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 017import org.apache.juneau.*; 018import org.apache.juneau.internal.*; 019 020/** 021 * Subclass of {@link SerializerSession} for stream-based serializers. 022 * 023 * <h5 class='topic'>Description</h5> 024 * 025 * This class is the parent class of all byte-based serializers. 026 * <br>It has 1 abstract method to implement... 027 * <ul> 028 * <li>{@link #doSerialize(SerializerPipe, Object)} 029 * </ul> 030 */ 031public abstract class OutputStreamSerializerSession extends SerializerSession { 032 033 private final OutputStreamSerializer ctx; 034 035 /** 036 * Create a new session using properties specified in the context. 037 * 038 * @param ctx 039 * The context creating this session object. 040 * The context contains all the configuration settings for this object. 041 * @param args 042 * Runtime arguments. 043 * These specify session-level information such as locale and URI context. 044 * It also include session-level properties that override the properties defined on the bean and 045 * serializer contexts. 046 */ 047 protected OutputStreamSerializerSession(OutputStreamSerializer ctx, SerializerSessionArgs args) { 048 super(ctx, args); 049 this.ctx = ctx; 050 } 051 052 /** 053 * Constructor for sessions that don't require context. 054 * 055 * @param args 056 * Runtime session arguments. 057 */ 058 protected OutputStreamSerializerSession(SerializerSessionArgs args) { 059 this(OutputStreamSerializer.DEFAULT, args); 060 } 061 062 @Override /* SerializerSession */ 063 public final boolean isWriterSerializer() { 064 return false; 065 } 066 067 /** 068 * Convenience method for serializing an object to a <code><jk>byte</jk></code>. 069 * 070 * @param o The object to serialize. 071 * @return The output serialized to a byte array. 072 * @throws SerializeException If a problem occurred trying to convert the output. 073 */ 074 @Override /* SerializerSession */ 075 public final byte[] serialize(Object o) throws SerializeException { 076 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 077 serialize(o, baos); 078 return baos.toByteArray(); 079 } 080 081 @Override /* SerializerSession */ 082 public final String serializeToString(Object o) throws SerializeException { 083 byte[] b = serialize(o); 084 switch(getBinaryFormat()) { 085 case SPACED_HEX: return StringUtils.toSpacedHex(b); 086 case HEX: return isUseWhitespace() ? StringUtils.toSpacedHex(b) : StringUtils.toHex(b); 087 case BASE64: return StringUtils.base64Encode(b); 088 default: return null; 089 } 090 } 091 092 //----------------------------------------------------------------------------------------------------------------- 093 // Properties 094 //----------------------------------------------------------------------------------------------------------------- 095 096 /** 097 * Configuration property: Binary output format. 098 * 099 * @see OutputStreamSerializer#OSSERIALIZER_binaryFormat 100 * @return 101 * The format to use for the {@link #serializeToString(Object)} method on stream-based serializers when converting byte arrays to strings. 102 */ 103 protected final BinaryFormat getBinaryFormat() { 104 return ctx.getBinaryFormat(); 105 } 106}