001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027package org.apache.http.nio.util; 028 029import java.io.IOException; 030 031import org.apache.http.nio.ContentDecoder; 032 033/** 034 * Basic implementation of the {@link ContentInputBuffer} interface. 035 * <p> 036 * This class is not thread safe. 037 * 038 * @since 4.0 039 */ 040public class SimpleInputBuffer extends ExpandableBuffer implements ContentInputBuffer { 041 042 private boolean endOfStream = false; 043 044 public SimpleInputBuffer(final int buffersize, final ByteBufferAllocator allocator) { 045 super(buffersize, allocator); 046 } 047 048 /** 049 * @since 4.3 050 */ 051 public SimpleInputBuffer(final int buffersize) { 052 this(buffersize, HeapByteBufferAllocator.INSTANCE); 053 } 054 055 @Override 056 public void reset() { 057 this.endOfStream = false; 058 super.clear(); 059 } 060 061 @Override 062 public int consumeContent(final ContentDecoder decoder) throws IOException { 063 setInputMode(); 064 int totalRead = 0; 065 int bytesRead; 066 while ((bytesRead = decoder.read(this.buffer)) != -1) { 067 if (bytesRead == 0) { 068 if (!this.buffer.hasRemaining()) { 069 expand(); 070 } else { 071 break; 072 } 073 } else { 074 totalRead += bytesRead; 075 } 076 } 077 if (bytesRead == -1 || decoder.isCompleted()) { 078 this.endOfStream = true; 079 } 080 return totalRead; 081 } 082 083 public boolean isEndOfStream() { 084 return !hasData() && this.endOfStream; 085 } 086 087 @Override 088 public int read() throws IOException { 089 if (isEndOfStream()) { 090 return -1; 091 } 092 setOutputMode(); 093 return this.buffer.get() & 0xff; 094 } 095 096 @Override 097 public int read(final byte[] b, final int off, final int len) throws IOException { 098 if (isEndOfStream()) { 099 return -1; 100 } 101 if (b == null) { 102 return 0; 103 } 104 setOutputMode(); 105 int chunk = len; 106 if (chunk > this.buffer.remaining()) { 107 chunk = this.buffer.remaining(); 108 } 109 this.buffer.get(b, off, chunk); 110 return chunk; 111 } 112 113 public int read(final byte[] b) throws IOException { 114 if (isEndOfStream()) { 115 return -1; 116 } 117 if (b == null) { 118 return 0; 119 } 120 return read(b, 0, b.length); 121 } 122 123 public void shutdown() { 124 this.endOfStream = true; 125 } 126 127}