001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.parser; 018 019import static org.apache.juneau.commons.utils.AssertionUtils.*; 020 021import java.io.*; 022import java.lang.reflect.*; 023import java.util.*; 024import java.util.function.*; 025 026import org.apache.juneau.*; 027import org.apache.juneau.httppart.*; 028 029/** 030 * Subclass of parser session objects for byte-based parsers. 031 * 032 * <h5 class='section'>Notes:</h5><ul> 033 * <li class='warn'>This class is not thread safe and is typically discarded after one use. 034 * </ul> 035 * 036 * <h5 class='section'>See Also:</h5><ul> 037 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/SerializersAndParsers">Serializers and Parsers</a> 038 * </ul> 039 */ 040public class InputStreamParserSession extends ParserSession { 041 /** 042 * Builder class. 043 */ 044 public static class Builder extends ParserSession.Builder { 045 046 private InputStreamParser ctx; 047 048 /** 049 * Constructor 050 * 051 * @param ctx The context creating this session. 052 * <br>Cannot be <jk>null</jk>. 053 */ 054 protected Builder(InputStreamParser ctx) { 055 super(assertArgNotNull("ctx", ctx)); 056 this.ctx = ctx; 057 } 058 059 @Override /* Overridden from Builder */ 060 public <T> Builder apply(Class<T> type, Consumer<T> apply) { 061 super.apply(type, apply); 062 return this; 063 } 064 065 @Override 066 public InputStreamParserSession build() { 067 return new InputStreamParserSession(this); 068 } 069 070 @Override /* Overridden from Builder */ 071 public Builder debug(Boolean value) { 072 super.debug(value); 073 return this; 074 } 075 076 @Override /* Overridden from Builder */ 077 public Builder javaMethod(Method value) { 078 super.javaMethod(value); 079 return this; 080 } 081 082 @Override /* Overridden from Builder */ 083 public Builder locale(Locale value) { 084 super.locale(value); 085 return this; 086 } 087 088 @Override /* Overridden from Builder */ 089 public Builder mediaType(MediaType value) { 090 super.mediaType(value); 091 return this; 092 } 093 094 @Override /* Overridden from Builder */ 095 public Builder mediaTypeDefault(MediaType value) { 096 super.mediaTypeDefault(value); 097 return this; 098 } 099 100 @Override /* Overridden from Builder */ 101 public Builder outer(Object value) { 102 super.outer(value); 103 return this; 104 } 105 106 @Override /* Overridden from Builder */ 107 public Builder properties(Map<String,Object> value) { 108 super.properties(value); 109 return this; 110 } 111 112 @Override /* Overridden from Builder */ 113 public Builder property(String key, Object value) { 114 super.property(key, value); 115 return this; 116 } 117 118 @Override /* Overridden from Builder */ 119 public Builder schema(HttpPartSchema value) { 120 super.schema(value); 121 return this; 122 } 123 124 @Override /* Overridden from Builder */ 125 public Builder schemaDefault(HttpPartSchema value) { 126 super.schemaDefault(value); 127 return this; 128 } 129 130 @Override /* Overridden from Builder */ 131 public Builder timeZone(TimeZone value) { 132 super.timeZone(value); 133 return this; 134 } 135 136 @Override /* Overridden from Builder */ 137 public Builder timeZoneDefault(TimeZone value) { 138 super.timeZoneDefault(value); 139 return this; 140 } 141 142 @Override /* Overridden from Builder */ 143 public Builder unmodifiable() { 144 super.unmodifiable(); 145 return this; 146 } 147 } 148 149 /** 150 * Creates a new builder for this object. 151 * 152 * @param ctx The context creating this session. 153 * <br>Cannot be <jk>null</jk>. 154 * @return A new builder. 155 */ 156 public static Builder create(InputStreamParser ctx) { 157 return new Builder(assertArgNotNull("ctx", ctx)); 158 } 159 160 private final InputStreamParser ctx; 161 162 /** 163 * Constructor. 164 * 165 * @param builder The builder for this object. 166 */ 167 protected InputStreamParserSession(Builder builder) { 168 super(builder); 169 this.ctx = builder.ctx; 170 } 171 172 /** 173 * Wraps the specified input object into a {@link ParserPipe} object so that it can be easily converted into 174 * a stream or reader. 175 * 176 * @param input 177 * The input. 178 * <br>This can be any of the following types: 179 * <ul> 180 * <li><jk>null</jk> 181 * <li>{@link InputStream} 182 * <li><code><jk>byte</jk>[]</code> 183 * <li>{@link File} 184 * <li>{@link CharSequence} containing encoded bytes according to the {@link InputStreamParser.Builder#binaryFormat(BinaryFormat)} setting. 185 * </ul> 186 * @return 187 * A new {@link ParserPipe} wrapper around the specified input object. 188 */ 189 @SuppressWarnings("resource") 190 @Override /* Overridden from ParserSession */ 191 public final ParserPipe createPipe(Object input) { 192 return setPipe(new ParserPipe(input, isDebug(), ctx.isAutoCloseStreams(), ctx.isUnbuffered(), ctx.getBinaryFormat())); 193 } 194 195 @Override /* Overridden from ParserSession */ 196 public final boolean isReaderParser() { return false; } 197 198 /** 199 * Binary input format. 200 * 201 * @see InputStreamParser.Builder#binaryFormat(BinaryFormat) 202 * @return 203 * The format to use when converting strings to byte arrays. 204 */ 205 protected final BinaryFormat getBinaryFormat() { return ctx.getBinaryFormat(); } 206}