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.parser;
014
015import java.io.*;
016
017import org.apache.juneau.*;
018
019/**
020 * Subclass of parser session objects for byte-based parsers.
021 *
022 * <p>
023 * This class is NOT thread safe.  It is typically discarded after one-time use.
024 */
025public abstract class InputStreamParserSession extends ParserSession {
026
027   private final InputStreamParser ctx;
028
029   /**
030    * Create a new session using properties specified in the context.
031    *
032    * @param ctx
033    *    The context creating this session object.
034    *    The context contains all the configuration settings for this object.
035    * @param args
036    *    Runtime session arguments.
037    */
038   protected InputStreamParserSession(InputStreamParser ctx, ParserSessionArgs args) {
039      super(ctx, args);
040      this.ctx = ctx;
041   }
042
043   /**
044    * Constructor for sessions that don't require context.
045    *
046    * @param args
047    *    Runtime session arguments.
048    */
049   protected InputStreamParserSession(ParserSessionArgs args) {
050      this(InputStreamParser.DEFAULT, args);
051   }
052
053   @Override /* ParserSession */
054   public final boolean isReaderParser() {
055      return false;
056   }
057
058   /**
059    * Wraps the specified input object into a {@link ParserPipe} object so that it can be easily converted into
060    * a stream or reader.
061    *
062    * @param input
063    *    The input.
064    *    </ul>
065    *    <br>This can be any of the following types:
066    *    <ul>
067    *       <li><jk>null</jk>
068    *       <li>{@link InputStream}
069    *       <li><code><jk>byte</jk>[]</code>
070    *       <li>{@link File}
071    *       <li>{@link CharSequence} containing encoded bytes according to the {@link InputStreamParser#ISPARSER_binaryFormat} setting.
072    *    </ul>
073    * @return
074    *    A new {@link ParserPipe} wrapper around the specified input object.
075    */
076   @SuppressWarnings("resource")
077   @Override /* ParserSession */
078   public final ParserPipe createPipe(Object input) {
079      return setPipe(new ParserPipe(input, isDebug(), ctx.isAutoCloseStreams(), ctx.isUnbuffered(), ctx.getBinaryFormat()));
080   }
081
082   //-----------------------------------------------------------------------------------------------------------------
083   // Properties
084   //-----------------------------------------------------------------------------------------------------------------
085
086   /**
087    * Configuration property:  Binary input format.
088    *
089    * @see InputStreamParser#ISPARSER_binaryFormat
090    * @return
091    *    The format to use when converting strings to byte arrays.
092    */
093   protected final BinaryFormat getBinaryFormat() {
094      return ctx.getBinaryFormat();
095   }
096
097   @Override /* Session */
098   public ObjectMap asMap() {
099      return super.asMap()
100         .append("InputStreamParserSession", new ObjectMap()
101         );
102   }
103}