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