17ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey/*
27ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey * Copyright (C) 2013 The Android Open Source Project
37ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey *
47ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
57ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey * you may not use this file except in compliance with the License.
67ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey * You may obtain a copy of the License at
77ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey *
87ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
97ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey *
107ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey * Unless required by applicable law or agreed to in writing, software
117ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
127ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey * See the License for the specific language governing permissions and
147ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey * limitations under the License.
157ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey */
167ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey
177ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkeypackage com.android.providers.downloads;
187ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey
197ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkeyimport java.io.InputStream;
207ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkeyimport java.util.Arrays;
217ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey
227ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey/**
237ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey * Provides fake data for large transfers.
247ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey */
257ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkeypublic class FakeInputStream extends InputStream {
267ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey    private long mRemaining;
277ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey
287ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey    public FakeInputStream(long length) {
297ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey        mRemaining = length;
307ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey    }
317ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey
327ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey    @Override
337ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey    public int read() {
347ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey        final int value;
357ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey        if (mRemaining > 0) {
367ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey            mRemaining--;
377ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey            return 0;
387ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey        } else {
397ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey            return -1;
407ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey        }
417ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey    }
427ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey
437ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey    @Override
447ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey    public int read(byte[] buffer, int offset, int length) {
457ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey        Arrays.checkOffsetAndCount(buffer.length, offset, length);
467ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey
477ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey        if (length > mRemaining) {
487ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey            length = (int) mRemaining;
497ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey        }
507ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey        mRemaining -= length;
517ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey
527ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey        if (length == 0) {
537ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey            return -1;
547ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey        } else {
557ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey            return length;
567ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey        }
577ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey    }
587ce206b2108714035eeed29c1dc268a3f1ccf359Jeff Sharkey}
59