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 static org.apache.juneau.internal.StringUtils.*; 016 017import org.apache.juneau.*; 018 019/** 020 * Subclass of {@link Serializer} for byte-based serializers. 021 */ 022public abstract class OutputStreamSerializer extends Serializer { 023 024 /** 025 * Constructor. 026 * 027 * @param ps 028 * The property store containing all the settings for this object. 029 * @param produces 030 * The media type that this serializer produces. 031 * @param accept 032 * The accept media types that the serializer can handle. 033 * <p> 034 * Can contain meta-characters per the <code>media-type</code> specification of 035 * <a class="doclink" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1">RFC2616/14.1</a> 036 * <p> 037 * If empty, then assumes the only media type supported is <code>produces</code>. 038 * <p> 039 * For example, if this serializer produces <js>"application/json"</js> but should handle media types of 040 * <js>"application/json"</js> and <js>"text/json"</js>, then the arguments should be: 041 * <p class='bcode'> 042 * <jk>super</jk>(ps, <js>"application/json"</js>, <js>"application/json"</js>, <js>"text/json"</js>); 043 * </p> 044 * <br>...or... 045 * <p class='bcode'> 046 * <jk>super</jk>(ps, <js>"application/json"</js>, <js>"*​/json"</js>); 047 * </p> 048 */ 049 protected OutputStreamSerializer(PropertyStore ps, String produces, String...accept) { 050 super(ps, produces, accept); 051 } 052 053 054 //-------------------------------------------------------------------------------- 055 // Abstract methods 056 //-------------------------------------------------------------------------------- 057 058 @Override /* SerializerSession */ 059 public abstract OutputStreamSerializerSession createSession(SerializerSessionArgs args); 060 061 062 //-------------------------------------------------------------------------------- 063 // Other methods 064 //-------------------------------------------------------------------------------- 065 066 @Override /* Serializer */ 067 public final boolean isWriterSerializer() { 068 return false; 069 } 070 071 /** 072 * Convenience method for serializing an object to a <code><jk>byte</jk></code>. 073 * 074 * @param o The object to serialize. 075 * @return The output serialized to a byte array. 076 * @throws SerializeException If a problem occurred trying to convert the output. 077 */ 078 @Override 079 public final byte[] serialize(Object o) throws SerializeException { 080 return createSession(createDefaultSessionArgs()).serialize(o); 081 } 082 083 /** 084 * Convenience method for serializing an object to a hex-encoded String. 085 * 086 * @param o The object to serialize. 087 * @return The output serialized to a hex-encoded string. 088 * @throws SerializeException If a problem occurred trying to convert the output. 089 */ 090 public final String serializeToHex(Object o) throws SerializeException { 091 return toHex(serialize(o)); 092 } 093}