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