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.*; 020 021/** 022 * Input stream meant to be used as input for stream-based parsers. 023 * 024 * <p> 025 * Keeps track of current byte position. 026 * 027 * <h5 class='section'>Notes:</h5><ul> 028 * <li class='warn'>This class is not thread safe. 029 * </ul> 030 * 031 * <h5 class='section'>See Also:</h5><ul> 032 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/SerializersAndParsers">Serializers and Parsers</a> 033 * </ul> 034 */ 035public class ParserInputStream extends InputStream implements Positionable { 036 037 private final InputStream is; 038 int pos = 0; 039 040 /** 041 * Constructor. 042 * 043 * @param pipe The parser input. 044 * @throws IOException Thrown by underlying stream. 045 */ 046 protected ParserInputStream(ParserPipe pipe) throws IOException { 047 this.is = pipe.getInputStream(); 048 pipe.setPositionable(this); 049 } 050 051 @Override /* InputStream */ 052 public int read() throws IOException { 053 int i = is.read(); 054 if (i > 0) 055 pos++; 056 return i; 057 } 058 059 @Override /* Positionable */ 060 public Position getPosition() { 061 return new Position(pos); 062 } 063}