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.*;
016import java.lang.reflect.*;
017import java.util.*;
018import java.util.function.*;
019
020import org.apache.juneau.*;
021import org.apache.juneau.httppart.*;
022import org.apache.juneau.internal.*;
023
024/**
025 * Subclass of parser session objects for byte-based parsers.
026 *
027 * <h5 class='section'>Notes:</h5><ul>
028 *    <li class='warn'>This class is not thread safe and is typically discarded after one use.
029 * </ul>
030 *
031 * <h5 class='section'>See Also:</h5><ul>
032 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.SerializersAndParsers">Serializers and Parsers</a>
033 * </ul>
034 */
035public class InputStreamParserSession extends ParserSession {
036
037   //-------------------------------------------------------------------------------------------------------------------
038   // Static
039   //-------------------------------------------------------------------------------------------------------------------
040
041   /**
042    * Creates a new builder for this object.
043    *
044    * @param ctx The context creating this session.
045    * @return A new builder.
046    */
047   public static Builder create(InputStreamParser ctx) {
048      return new Builder(ctx);
049   }
050
051   //-------------------------------------------------------------------------------------------------------------------
052   // Builder
053   //-------------------------------------------------------------------------------------------------------------------
054
055   /**
056    * Builder class.
057    */
058   @FluentSetters
059   public static class Builder extends ParserSession.Builder {
060
061      InputStreamParser ctx;
062
063      /**
064       * Constructor
065       *
066       * @param ctx The context creating this session.
067       */
068      protected Builder(InputStreamParser ctx) {
069         super(ctx);
070         this.ctx = ctx;
071      }
072
073      @Override
074      public InputStreamParserSession build() {
075         return new InputStreamParserSession(this);
076      }
077
078      // <FluentSetters>
079
080      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
081      public <T> Builder apply(Class<T> type, Consumer<T> apply) {
082         super.apply(type, apply);
083         return this;
084      }
085
086      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
087      public Builder debug(Boolean value) {
088         super.debug(value);
089         return this;
090      }
091
092      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
093      public Builder properties(Map<String,Object> value) {
094         super.properties(value);
095         return this;
096      }
097
098      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
099      public Builder property(String key, Object value) {
100         super.property(key, value);
101         return this;
102      }
103
104      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
105      public Builder unmodifiable() {
106         super.unmodifiable();
107         return this;
108      }
109
110      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
111      public Builder locale(Locale value) {
112         super.locale(value);
113         return this;
114      }
115
116      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
117      public Builder localeDefault(Locale value) {
118         super.localeDefault(value);
119         return this;
120      }
121
122      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
123      public Builder mediaType(MediaType value) {
124         super.mediaType(value);
125         return this;
126      }
127
128      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
129      public Builder mediaTypeDefault(MediaType value) {
130         super.mediaTypeDefault(value);
131         return this;
132      }
133
134      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
135      public Builder timeZone(TimeZone value) {
136         super.timeZone(value);
137         return this;
138      }
139
140      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
141      public Builder timeZoneDefault(TimeZone value) {
142         super.timeZoneDefault(value);
143         return this;
144      }
145
146      @Override /* GENERATED - org.apache.juneau.parser.ParserSession.Builder */
147      public Builder javaMethod(Method value) {
148         super.javaMethod(value);
149         return this;
150      }
151
152      @Override /* GENERATED - org.apache.juneau.parser.ParserSession.Builder */
153      public Builder outer(Object value) {
154         super.outer(value);
155         return this;
156      }
157
158      @Override /* GENERATED - org.apache.juneau.parser.ParserSession.Builder */
159      public Builder schema(HttpPartSchema value) {
160         super.schema(value);
161         return this;
162      }
163
164      @Override /* GENERATED - org.apache.juneau.parser.ParserSession.Builder */
165      public Builder schemaDefault(HttpPartSchema value) {
166         super.schemaDefault(value);
167         return this;
168      }
169
170      // </FluentSetters>
171   }
172
173   //-------------------------------------------------------------------------------------------------------------------
174   // Instance
175   //-------------------------------------------------------------------------------------------------------------------
176
177   private final InputStreamParser ctx;
178
179   /**
180    * Constructor.
181    *
182    * @param builder The builder for this object.
183    */
184   protected InputStreamParserSession(Builder builder) {
185      super(builder);
186      this.ctx = builder.ctx;
187   }
188
189   @Override /* ParserSession */
190   public final boolean isReaderParser() {
191      return false;
192   }
193
194   /**
195    * Wraps the specified input object into a {@link ParserPipe} object so that it can be easily converted into
196    * a stream or reader.
197    *
198    * @param input
199    *    The input.
200    *    <br>This can be any of the following types:
201    *    <ul>
202    *       <li><jk>null</jk>
203    *       <li>{@link InputStream}
204    *       <li><code><jk>byte</jk>[]</code>
205    *       <li>{@link File}
206    *       <li>{@link CharSequence} containing encoded bytes according to the {@link InputStreamParser.Builder#binaryFormat(BinaryFormat)} setting.
207    *    </ul>
208    * @return
209    *    A new {@link ParserPipe} wrapper around the specified input object.
210    */
211   @SuppressWarnings("resource")
212   @Override /* ParserSession */
213   public final ParserPipe createPipe(Object input) {
214      return setPipe(new ParserPipe(input, isDebug(), ctx.isAutoCloseStreams(), ctx.isUnbuffered(), ctx.getBinaryFormat()));
215   }
216
217   //-----------------------------------------------------------------------------------------------------------------
218   // Properties
219   //-----------------------------------------------------------------------------------------------------------------
220
221   /**
222    * Binary input format.
223    *
224    * @see InputStreamParser.Builder#binaryFormat(BinaryFormat)
225    * @return
226    *    The format to use when converting strings to byte arrays.
227    */
228   protected final BinaryFormat getBinaryFormat() {
229      return ctx.getBinaryFormat();
230   }
231}