1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package libcore.net.http; 18 19import java.io.IOException; 20import java.io.InputStream; 21import java.net.CacheRequest; 22import java.util.Arrays; 23 24/** 25 * An HTTP payload terminated by the end of the socket stream. 26 */ 27final class UnknownLengthHttpInputStream extends AbstractHttpInputStream { 28 private boolean inputExhausted; 29 30 UnknownLengthHttpInputStream(InputStream is, CacheRequest cacheRequest, 31 HttpEngine httpEngine) throws IOException { 32 super(is, httpEngine, cacheRequest); 33 } 34 35 @Override public int read(byte[] buffer, int offset, int count) throws IOException { 36 Arrays.checkOffsetAndCount(buffer.length, offset, count); 37 checkNotClosed(); 38 if (in == null || inputExhausted) { 39 return -1; 40 } 41 int read = in.read(buffer, offset, count); 42 if (read == -1) { 43 inputExhausted = true; 44 endOfInput(false); 45 return -1; 46 } 47 cacheWrite(buffer, offset, read); 48 return read; 49 } 50 51 @Override public int available() throws IOException { 52 checkNotClosed(); 53 return in == null ? 0 : in.available(); 54 } 55 56 @Override public void close() throws IOException { 57 if (closed) { 58 return; 59 } 60 closed = true; 61 if (!inputExhausted) { 62 unexpectedEndOfInput(); 63 } 64 } 65} 66