1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/embedder_support/android/util/input_stream_reader.h"
#include "components/embedder_support/android/util/input_stream.h"
#include "net/base/net_errors.h"
#include "net/http/http_byte_range.h"
namespace embedder_support {
InputStreamReader::InputStreamReader(InputStream* stream) : stream_(stream) {
DCHECK(stream);
}
InputStreamReader::~InputStreamReader() = default;
int InputStreamReader::Seek(const net::HttpByteRange& byte_range) {
int content_size = 0;
net::HttpByteRange verified_byte_range(byte_range);
int error_code = VerifyRequestedRange(&verified_byte_range, &content_size);
if (error_code != net::OK)
return error_code;
error_code = SkipToRequestedRange(verified_byte_range);
if (error_code != net::OK)
return error_code;
DCHECK_GE(content_size, 0);
return content_size;
}
int InputStreamReader::ReadRawData(net::IOBuffer* dest, int dest_size) {
if (!dest_size)
return 0;
DCHECK_GT(dest_size, 0);
int bytes_read = 0;
if (!stream_->Read(dest, dest_size, &bytes_read))
return net::ERR_FAILED;
else
return bytes_read;
}
int InputStreamReader::VerifyRequestedRange(net::HttpByteRange* byte_range,
int* content_size) {
DCHECK(content_size);
int32_t size = 0;
if (!stream_->BytesAvailable(&size))
return net::ERR_FAILED;
if (size <= 0)
return net::OK;
// Check that the requested range was valid.
if (!byte_range->ComputeBounds(size))
return net::ERR_REQUEST_RANGE_NOT_SATISFIABLE;
size =
byte_range->last_byte_position() - byte_range->first_byte_position() + 1;
DCHECK_GE(size, 0);
*content_size = size;
return net::OK;
}
int InputStreamReader::SkipToRequestedRange(
const net::HttpByteRange& byte_range) {
// Skip to the start of the requested data. This has to be done in a loop
// because the underlying InputStream is not guaranteed to skip the requested
// number of bytes.
if (byte_range.IsValid() && byte_range.first_byte_position() > 0) {
int64_t bytes_to_skip = byte_range.first_byte_position();
do {
int64_t skipped = 0;
if (!stream_->Skip(bytes_to_skip, &skipped))
return net::ERR_FAILED;
if (skipped <= 0)
return net::ERR_REQUEST_RANGE_NOT_SATISFIABLE;
DCHECK_LE(skipped, bytes_to_skip);
bytes_to_skip -= skipped;
} while (bytes_to_skip > 0);
}
return net::OK;
}
} // namespace embedder_support
|