Home | History | Annotate | Download | only in http
      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 
     17 package org.apache.harmony.luni.internal.net.www.protocol.http;
     18 
     19 import java.io.IOException;
     20 import java.io.InputStream;
     21 import java.net.CacheRequest;
     22 
     23 /**
     24  * An HTTP payload terminated by the end of the socket stream.
     25  */
     26 final class UnknownLengthHttpInputStream extends AbstractHttpInputStream {
     27     private boolean inputExhausted;
     28 
     29     UnknownLengthHttpInputStream(InputStream is, CacheRequest cacheRequest,
     30             HttpURLConnectionImpl httpURLConnection) throws IOException {
     31         super(is, httpURLConnection, cacheRequest);
     32     }
     33 
     34     @Override public int read(byte[] buffer, int offset, int count) throws IOException {
     35         checkBounds(buffer, offset, count);
     36         checkNotClosed();
     37         if (in == null || inputExhausted) {
     38             return -1;
     39         }
     40         int read = in.read(buffer, offset, count);
     41         if (read == -1) {
     42             inputExhausted = true;
     43             endOfInput(false);
     44             return -1;
     45         }
     46         cacheWrite(buffer, offset, read);
     47         return read;
     48     }
     49 
     50     @Override public int available() throws IOException {
     51         checkNotClosed();
     52         return in == null ? 0 : in.available();
     53     }
     54 
     55     @Override public void close() throws IOException {
     56         if (closed) {
     57             return;
     58         }
     59         closed = true;
     60         if (!inputExhausted) {
     61             unexpectedEndOfInput();
     62         }
     63     }
     64 }
     65