<commit_before>/*
 * inventory - an ear/war inventory recorder
 * Copyright 2016-2019 MeBigFatGuy.com
 * Copyright 2016-2019 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations
 * under the License.
 */
package com.mebigfatguy.inventory.utils;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class LengthLimitedInputStream extends InputStream {

    private InputStream parentStream;
    private long remainingLength;

    public LengthLimitedInputStream(InputStream parent, long length) {
        parentStream = parent;
        remainingLength = length;
    }

    @Override
    public int read() throws IOException {
        if (remainingLength == 0) {
            return -1;
        }
        int data = parentStream.read();
        --remainingLength;
        return data;
    }

    @Override
    public int read(byte[] b) throws IOException {
        return read(b, 0, b.length);
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        if (len > remainingLength) {
            len = (int) remainingLength;
        }

        if (len <= 0) {
            throw new EOFException("Read past the end of the stream");
        }

        int actLen = parentStream.read(b, off, len);
        remainingLength -= actLen;
        return actLen;
    }

    @Override
    public long skip(long n) throws IOException {
        if (remainingLength == 0) {
            throw new EOFException("Read past the end of the stream");
        }

        if (n > remainingLength) {
            remainingLength = 0;
            return remainingLength;
        }

        remainingLength -= n;
        return n;

    }

    @Override
    public int available() throws IOException {
        if (remainingLength > Integer.MAX_VALUE) {
            return Integer.MAX_VALUE;
        }

        return (int) remainingLength;
    }
<commit_msg>chore: fix LengthLimited handling of 0 length reads<commit_after>/*
 * inventory - an ear/war inventory recorder
 * Copyright 2016-2019 MeBigFatGuy.com
 * Copyright 2016-2019 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations
 * under the License.
 */
package com.mebigfatguy.inventory.utils;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class LengthLimitedInputStream extends InputStream {

    private InputStream parentStream;
    private long remainingLength;

    public LengthLimitedInputStream(InputStream parent, long length) {
        parentStream = parent;
        remainingLength = length;
    }

    @Override
    public int read() throws IOException {
        if (remainingLength == 0) {
            return -1;
        }
        int data = parentStream.read();
        --remainingLength;
        return data;
    }

    @Override
    public int read(byte[] b) throws IOException {
        return read(b, 0, b.length);
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        if (len > remainingLength) {
            len = (int) remainingLength;
        }

        if (len < 0) {
            throw new EOFException("Read past the end of the stream");
        }

        if (len == 0) {
        	return -1;
        }
        
        int actLen = parentStream.read(b, off, len);
        remainingLength -= actLen;
        return actLen;
    }

    @Override
    public long skip(long n) throws IOException {
        if (remainingLength == 0) {
            throw new EOFException("Read past the end of the stream");
        }

        if (n > remainingLength) {
            remainingLength = 0;
            return remainingLength;
        }

        remainingLength -= n;
        return n;

    }

    @Override
    public int available() throws IOException {
        if (remainingLength > Integer.MAX_VALUE) {
            return Integer.MAX_VALUE;
        }

        return (int) remainingLength;
    }

{'fraction_non_alphanumeric': 0.08326817826426897, 'fraction_numerical': 0.011532447224394058, 'mean_word_length': 2.4181696726786908, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5834069', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1328, 'n_words': 476}