154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson/*
254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * Copyright (C) 2012 The Android Open Source Project
354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson *
454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * Licensed under the Apache License, Version 2.0 (the "License");
554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * you may not use this file except in compliance with the License.
654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * You may obtain a copy of the License at
754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson *
854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson *      http://www.apache.org/licenses/LICENSE-2.0
954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson *
1054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * Unless required by applicable law or agreed to in writing, software
1154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * distributed under the License is distributed on an "AS IS" BASIS,
1254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * See the License for the specific language governing permissions and
1454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * limitations under the License.
1554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson */
1654cf3446000fdcf88a9e62724f7deb0282e98da1jwilsonpackage com.squareup.okhttp.internal.http;
1754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson
1854cf3446000fdcf88a9e62724f7deb0282e98da1jwilsonimport java.io.IOException;
1954cf3446000fdcf88a9e62724f7deb0282e98da1jwilsonimport java.io.InputStream;
2054cf3446000fdcf88a9e62724f7deb0282e98da1jwilsonimport java.net.CacheRequest;
2154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson
2254cf3446000fdcf88a9e62724f7deb0282e98da1jwilsonimport static com.squareup.okhttp.internal.Util.checkOffsetAndCount;
2354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson
2454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson/** An HTTP message body terminated by the end of the underlying stream. */
2554cf3446000fdcf88a9e62724f7deb0282e98da1jwilsonfinal class UnknownLengthHttpInputStream extends AbstractHttpInputStream {
2654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  private boolean inputExhausted;
2754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson
2854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  UnknownLengthHttpInputStream(InputStream is, CacheRequest cacheRequest, HttpEngine httpEngine)
2954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson      throws IOException {
3054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    super(is, httpEngine, cacheRequest);
3154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  }
3254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson
3354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  @Override public int read(byte[] buffer, int offset, int count) throws IOException {
3454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    checkOffsetAndCount(buffer.length, offset, count);
3554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    checkNotClosed();
3654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    if (in == null || inputExhausted) {
3754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson      return -1;
3854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    }
3954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    int read = in.read(buffer, offset, count);
4054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    if (read == -1) {
4154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson      inputExhausted = true;
4254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson      endOfInput(false);
4354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson      return -1;
4454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    }
4554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    cacheWrite(buffer, offset, read);
4654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    return read;
4754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  }
4854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson
4954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  @Override public int available() throws IOException {
5054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    checkNotClosed();
5154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    return in == null ? 0 : in.available();
5254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  }
5354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson
5454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  @Override public void close() throws IOException {
5554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    if (closed) {
5654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson      return;
5754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    }
5854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    closed = true;
5954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    if (!inputExhausted) {
6054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson      unexpectedEndOfInput();
6154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    }
6254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  }
6354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson}
64