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 @Override /* SerializerSession */ 068 protected SerializerPipe createPipe(Object output) { 069 return new SerializerPipe(output); 070 } 071 072 /** 073 * Convenience method for serializing an object to a <code><jk>byte</jk></code>. 074 * 075 * @param o The object to serialize. 076 * @return The output serialized to a byte array. 077 * @throws SerializeException If a problem occurred trying to convert the output. 078 */ 079 @Override /* SerializerSession */ 080 public final byte[] serialize(Object o) throws SerializeException { 081 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 082 try { 083 serialize(o, baos); 084 } catch (IOException e) { 085 throw new SerializeException(e); // Should never happen. 086 } 087 return baos.toByteArray(); 088 } 089 090 @Override /* SerializerSession */ 091 public final String serializeToString(Object o) throws SerializeException { 092 byte[] b = serialize(o); 093 switch(getBinaryFormat()) { 094 case SPACED_HEX: return StringUtils.toSpacedHex(b); 095 case HEX: return StringUtils.toHex(b); 096 case BASE64: return StringUtils.base64Encode(b); 097 default: return null; 098 } 099 } 100 101 //----------------------------------------------------------------------------------------------------------------- 102 // Properties 103 //----------------------------------------------------------------------------------------------------------------- 104 105 /** 106 * Configuration property: Binary output format. 107 * 108 * @see OutputStreamSerializer#OSSERIALIZER_binaryFormat 109 * @return 110 * The format to use for the {@link #serializeToString(Object)} method on stream-based serializers when converting byte arrays to strings. 111 */ 112 protected final BinaryFormat getBinaryFormat() { 113 return ctx.getBinaryFormat(); 114 } 115 116 //----------------------------------------------------------------------------------------------------------------- 117 // Other methods 118 //----------------------------------------------------------------------------------------------------------------- 119 120 @Override /* Session */ 121 public ObjectMap toMap() { 122 return super.toMap() 123 .append("OutputStreamSerializerSession", new DefaultFilteringObjectMap() 124 ); 125 } 126}