16906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson/* 26906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson * Copyright (C) 2010 The Android Open Source Project 36906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson * 46906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License"); 56906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson * you may not use this file except in compliance with the License. 66906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson * You may obtain a copy of the License at 76906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson * 86906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 96906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson * 106906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson * Unless required by applicable law or agreed to in writing, software 116906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 126906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 136906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson * See the License for the specific language governing permissions and 146906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson * limitations under the License. 156906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson */ 166906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson 175aafac4db69e6d087c512cdfa5c7c0e2f1611681Jesse Wilsonpackage libcore.net.http; 186906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson 196906b0c12dcf3216883d0373973a252812a20d32Jesse Wilsonimport java.io.IOException; 206906b0c12dcf3216883d0373973a252812a20d32Jesse Wilsonimport java.io.InputStream; 216906b0c12dcf3216883d0373973a252812a20d32Jesse Wilsonimport java.net.CacheRequest; 22b9cc455ed89df1a0cf4186c92b352c9649995d96Elliott Hughesimport java.util.Arrays; 236906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson 246906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson/** 256906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson * An HTTP payload terminated by the end of the socket stream. 266906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson */ 276906b0c12dcf3216883d0373973a252812a20d32Jesse Wilsonfinal class UnknownLengthHttpInputStream extends AbstractHttpInputStream { 286906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson private boolean inputExhausted; 296906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson 306906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson UnknownLengthHttpInputStream(InputStream is, CacheRequest cacheRequest, 31953df613522e12a418cb7cb73248594d6c9f53d4Jesse Wilson HttpEngine httpEngine) throws IOException { 32953df613522e12a418cb7cb73248594d6c9f53d4Jesse Wilson super(is, httpEngine, cacheRequest); 336906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } 346906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson 35da289bcd0a9e207cc03c752f7c21c9004056e179Jesse Wilson @Override public int read(byte[] buffer, int offset, int count) throws IOException { 36b9cc455ed89df1a0cf4186c92b352c9649995d96Elliott Hughes Arrays.checkOffsetAndCount(buffer.length, offset, count); 376906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson checkNotClosed(); 38bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson if (in == null || inputExhausted) { 396906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson return -1; 406906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } 41da289bcd0a9e207cc03c752f7c21c9004056e179Jesse Wilson int read = in.read(buffer, offset, count); 42da289bcd0a9e207cc03c752f7c21c9004056e179Jesse Wilson if (read == -1) { 436906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson inputExhausted = true; 44c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson endOfInput(false); 456906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson return -1; 466906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } 47da289bcd0a9e207cc03c752f7c21c9004056e179Jesse Wilson cacheWrite(buffer, offset, read); 48da289bcd0a9e207cc03c752f7c21c9004056e179Jesse Wilson return read; 496906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } 506906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson 516906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson @Override public int available() throws IOException { 526906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson checkNotClosed(); 536906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson return in == null ? 0 : in.available(); 546906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } 556906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson 566906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson @Override public void close() throws IOException { 576906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson if (closed) { 586906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson return; 596906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } 606906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson closed = true; 616906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson if (!inputExhausted) { 626906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson unexpectedEndOfInput(); 636906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } 646906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } 656906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson} 66