182a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson/*
282a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson *  Licensed to the Apache Software Foundation (ASF) under one or more
382a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson *  contributor license agreements.  See the NOTICE file distributed with
482a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson *  this work for additional information regarding copyright ownership.
582a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson *  The ASF licenses this file to You under the Apache License, Version 2.0
682a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson *  (the "License"); you may not use this file except in compliance with
782a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson *  the License.  You may obtain a copy of the License at
882a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson *
982a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson *     http://www.apache.org/licenses/LICENSE-2.0
1082a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson *
1182a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson *  Unless required by applicable law or agreed to in writing, software
1282a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson *  distributed under the License is distributed on an "AS IS" BASIS,
1382a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1482a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson *  See the License for the specific language governing permissions and
1582a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson *  limitations under the License.
1682a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson */
1782a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson
1882a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilsonpackage tests.support;
1982a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson
2082a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilsonimport java.io.FilterReader;
2182a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilsonimport java.io.IOException;
2282a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilsonimport java.io.Reader;
2382a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson
2482a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson/**
2582a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson * A reader that always throws after a predetermined number of bytes have been
2682a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson * read.
2782a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson */
2882a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilsonpublic class ThrowingReader extends FilterReader {
2982a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson
3082a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson    private int total = 0;
3182a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson    private int throwAt;
3282a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson
3382a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson    public ThrowingReader(Reader in, int throwAt) {
3482a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson        super(in);
3582a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson        this.throwAt = throwAt;
3682a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson    }
3782a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson
3882a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson    @Override public int read() throws IOException {
3982a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson        explodeIfNecessary();
4082a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson        int result = super.read();
4182a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson        total++;
4282a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson        return result;
4382a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson    }
4482a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson
4582a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson    @Override public int read(char[] buf, int offset, int count)
4682a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson            throws IOException {
4782a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson        explodeIfNecessary();
4882a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson
4982a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson        if (total < throwAt) {
5082a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson            count = Math.min(count, (throwAt - total));
5182a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson        }
5282a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson
5382a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson        int returned = super.read(buf, offset, count);
5482a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson        total += returned;
5582a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson        return returned;
5682a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson    }
5782a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson
5882a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson    private void explodeIfNecessary() throws IOException {
5982a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson        if (total == throwAt) {
6082a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson            throwAt = Integer.MAX_VALUE;
6182a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson            throw new IOException();
6282a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson        }
6382a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson    }
6482a2dfcd2281c2f8042fd5f09c98bea1e728530fJesse Wilson}
65