<commit_before>/*
 * Copyright 2015 Kaazing Corporation
 *
 * 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 uk.co.real_logic.aeron.tools;

import java.io.InputStream;
import java.util.zip.CRC32;

import uk.co.real_logic.agrona.DirectBuffer;
import uk.co.real_logic.agrona.concurrent.UnsafeBuffer;

public class MessageStream
{
    /* Message header offsets for verifiable message headers. */
    private static final int MAGIC_OFFSET = 0;
    private static final int MESSAGE_CHECKSUM_OFFSET = 4;
    private static final int SEQUENCE_NUMBER_OFFSET = 8;
    private final int messageOffset; /* Either 0 or 16, depending on if a verifiable message header is used. */

    private static final int HEADER_LENGTH = 16;

    private static final int MAGIC = 0x0dd01221; /* That's "2112d00d" when taken a byte at a time... */
    private static final int MAGIC_END = 0xbeba1221; /* That's "2112babe" when taken a byte at a time... */

    private static final int HEX_PRINT_WIDTH = 16;

    private final int minSize;
    private final int maxSize;
    private final boolean verifiable;

    private InputStream inputStream;
    private final boolean inputStreamIsRandom;
    private byte[] inputStreamBytes;

    private long sequenceNumber = -1;
    private long messageCount = 0;
    private boolean active = true;

    private static final ThreadLocalCRC32 MSG_CHECKSUM = new ThreadLocalCRC32();

    private static class ThreadLocalCRC32 extends ThreadLocal<CRC32>
    {
        protected CRC32 initialValue()
        {
            return new CRC32();
        }
    }

    public MessageStream(final int size) throws Exception
    {
        this(size, size, true, null);
    }

    public MessageStream(final int minSize, final int maxSize) throws Exception
    {
        this(minSize, maxSize, true, null);
    }

    public MessageStream(final int size, final InputStream inputStream) throws Exception
    {
        this(size, size, true, inputStream);
    }

    public MessageStream(final int size, final boolean verifiable, final InputStream inputStream) throws Exception
    {
        this(size, size, verifiable, inputStream);
    }

    public MessageStream(final int minSize, final int maxSize, final boolean verifiable) throws Exception
    {
        this(minSize, maxSize, verifiable, null);
    }

    public MessageStream(final int minSize, final int maxSize, final boolean verifiable,
        final InputStream inputStream) throws Exception
    {
        if (this.inputStream == null)
        {
            // When no input stream is supplied, use random generator.
            this.inputStream = new RandomInputStream();
            this.inputStreamIsRandom = true;
        }
        else
        {
            this.inputStream = inputStream;
            this.inputStreamIsRandom = false;
        }
        if (minSize < 0)
        {
            throw new Exception("MessageStream minimum message size must be 0 or greater.");<commit_msg>Fixed bug where a random InputStream was always used instead of possibly a given InputStream.<commit_after>/*
 * Copyright 2015 Kaazing Corporation
 *
 * 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 uk.co.real_logic.aeron.tools;

import java.io.InputStream;
import java.util.zip.CRC32;

import uk.co.real_logic.agrona.DirectBuffer;
import uk.co.real_logic.agrona.concurrent.UnsafeBuffer;

public class MessageStream
{
    /* Message header offsets for verifiable message headers. */
    private static final int MAGIC_OFFSET = 0;
    private static final int MESSAGE_CHECKSUM_OFFSET = 4;
    private static final int SEQUENCE_NUMBER_OFFSET = 8;
    private final int messageOffset; /* Either 0 or 16, depending on if a verifiable message header is used. */

    private static final int HEADER_LENGTH = 16;

    private static final int MAGIC = 0x0dd01221; /* That's "2112d00d" when taken a byte at a time... */
    private static final int MAGIC_END = 0xbeba1221; /* That's "2112babe" when taken a byte at a time... */

    private static final int HEX_PRINT_WIDTH = 16;

    private final int minSize;
    private final int maxSize;
    private final boolean verifiable;

    private InputStream inputStream;
    private final boolean inputStreamIsRandom;
    private byte[] inputStreamBytes;

    private long sequenceNumber = -1;
    private long messageCount = 0;
    private boolean active = true;

    private static final ThreadLocalCRC32 MSG_CHECKSUM = new ThreadLocalCRC32();

    private static class ThreadLocalCRC32 extends ThreadLocal<CRC32>
    {
        protected CRC32 initialValue()
        {
            return new CRC32();
        }
    }

    public MessageStream(final int size) throws Exception
    {
        this(size, size, true, null);
    }

    public MessageStream(final int minSize, final int maxSize) throws Exception
    {
        this(minSize, maxSize, true, null);
    }

    public MessageStream(final int size, final InputStream inputStream) throws Exception
    {
        this(size, size, true, inputStream);
    }

    public MessageStream(final int size, final boolean verifiable, final InputStream inputStream) throws Exception
    {
        this(size, size, verifiable, inputStream);
    }

    public MessageStream(final int minSize, final int maxSize, final boolean verifiable) throws Exception
    {
        this(minSize, maxSize, verifiable, null);
    }

    public MessageStream(final int minSize, final int maxSize, final boolean verifiable,
        final InputStream inputStream) throws Exception
    {
        if (inputStream == null)
        {
            // When no input stream is supplied, use random generator.
            this.inputStream = new RandomInputStream();
            this.inputStreamIsRandom = true;
        }
        else
        {
            this.inputStream = inputStream;
            this.inputStreamIsRandom = false;
        }
        if (minSize < 0)
        {
            throw new Exception("MessageStream minimum message size must be 0 or greater.");
{'fraction_non_alphanumeric': 0.06933942074475674, 'fraction_numerical': 0.0165501498073905, 'mean_word_length': 2.962690785754664, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '5397854', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1800, 'n_words': 717}