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.marshall; 014 015import org.apache.juneau.parser.*; 016import org.apache.juneau.serializer.*; 017 018/** 019 * A subclass of {@link Marshall} for stream-based serializers and parsers. 020 * 021 * <ul class='seealso'> 022 * <li class='link'>{@doc juneau-marshall.Marshalls} 023 * </ul> 024 */ 025public abstract class StreamMarshall extends Marshall { 026 027 private final OutputStreamSerializer s; 028 029 /** 030 * Constructor. 031 * 032 * @param s 033 * The serializer to use for serializing output. 034 * <br>Must not be <jk>null</jk>. 035 * @param p 036 * The parser to use for parsing input. 037 * <br>Must not be <jk>null</jk>. 038 */ 039 protected StreamMarshall(OutputStreamSerializer s, InputStreamParser p) { 040 super(s, p); 041 this.s = s; 042 } 043 044 /** 045 * Serializes a POJO directly to a <code><jk>byte</jk>[]</code>. 046 * 047 * @param o The object to serialize. 048 * @return 049 * The serialized object. 050 * @throws SerializeException If a problem occurred trying to convert the output. 051 */ 052 @Override /* Serializer */ 053 public final byte[] write(Object o) throws SerializeException { 054 return s.createSession(null).serialize(o); 055 } 056}