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    *    <br>This can be any of the following types:
065    *    <ul>
066    *       <li><jk>null</jk>
067    *       <li>{@link InputStream}
068    *       <li><code><jk>byte</jk>[]</code>
069    *       <li>{@link File}
070    *       <li>{@link CharSequence} containing encoded bytes according to the {@link InputStreamParser#ISPARSER_binaryFormat} setting.
071    *    </ul>
072    * @return
073    *    A new {@link ParserPipe} wrapper around the specified input object.
074    */
075   @SuppressWarnings("resource")
076   @Override /* ParserSession */
077   public final ParserPipe createPipe(Object input) {
078      return setPipe(new ParserPipe(input, isDebug(), ctx.isAutoCloseStreams(), ctx.isUnbuffered(), ctx.getBinaryFormat()));
079   }
080
081   //-----------------------------------------------------------------------------------------------------------------
082   // Properties
083   //-----------------------------------------------------------------------------------------------------------------
084
085   /**
086    * Configuration property:  Binary input format.
087    *
088    * @see InputStreamParser#ISPARSER_binaryFormat
089    * @return
090    *    The format to use when converting strings to byte arrays.
091    */
092   protected final BinaryFormat getBinaryFormat() {
093      return ctx.getBinaryFormat();
094   }
095
096   //-----------------------------------------------------------------------------------------------------------------
097   // Other methods
098   //-----------------------------------------------------------------------------------------------------------------
099
100   @Override /* Session */
101   public ObjectMap toMap() {
102      return super.toMap()
103         .append("InputStreamParserSession", new DefaultFilteringObjectMap()
104         );
105   }
106}