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.*; 018import org.apache.juneau.collections.*; 019 020/** 021 * Subclass of parser session objects for byte-based parsers. 022 * 023 * <p> 024 * This class is NOT thread safe. It is typically discarded after one-time use. 025 */ 026public abstract class InputStreamParserSession extends ParserSession { 027 028 private final InputStreamParser ctx; 029 030 /** 031 * Create a new session using properties specified in the context. 032 * 033 * @param ctx 034 * The context creating this session object. 035 * The context contains all the configuration settings for this object. 036 * @param args 037 * Runtime session arguments. 038 */ 039 protected InputStreamParserSession(InputStreamParser ctx, ParserSessionArgs args) { 040 super(ctx, args); 041 this.ctx = ctx; 042 } 043 044 /** 045 * Constructor for sessions that don't require context. 046 * 047 * @param args 048 * Runtime session arguments. 049 */ 050 protected InputStreamParserSession(ParserSessionArgs args) { 051 this(InputStreamParser.DEFAULT, args); 052 } 053 054 @Override /* ParserSession */ 055 public final boolean isReaderParser() { 056 return false; 057 } 058 059 /** 060 * Wraps the specified input object into a {@link ParserPipe} object so that it can be easily converted into 061 * a stream or reader. 062 * 063 * @param input 064 * The input. 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 //----------------------------------------------------------------------------------------------------------------- 098 // Other methods 099 //----------------------------------------------------------------------------------------------------------------- 100 101 @Override /* Session */ 102 public OMap toMap() { 103 return super.toMap() 104 .a("InputStreamParserSession", new DefaultFilteringOMap() 105 ); 106 } 107}