Class DataGeneratorInputStream

java.lang.Object
java.io.InputStream
com.cedarsoftware.util.DataGeneratorInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class DataGeneratorInputStream extends InputStream
A flexible InputStream that generates data on-the-fly using various generation strategies. This class is ideal for testing code that handles large streams, generating synthetic data, or creating pattern-based input without consuming memory to store the data.

Data is generated as needed and immediately discarded, making it memory-efficient for testing with very large stream sizes.

Example usage:


 // Random bytes
 try (InputStream input = DataGeneratorInputStream.withRandomBytes(1024 * 1024)) {
     processStream(input);
 }

 // Repeating pattern
 try (InputStream input = DataGeneratorInputStream.withRepeatingPattern(1024, "Hello")) {
     processStream(input);
 }

 // Custom generator
 try (InputStream input = DataGeneratorInputStream.withGenerator(1024, () -> 42)) {
     processStream(input);
 }
 
Author:
John DeRegnaucourt (jdereg@gmail.com)
Copyright (c) Cedar Software LLC

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

License

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.
  • Constructor Details

    • DataGeneratorInputStream

      public DataGeneratorInputStream(long size, IntSupplier generator)
      Creates a DataGeneratorInputStream with the specified size and generator.
      Parameters:
      size - the number of bytes this stream will provide before returning -1 (EOF)
      generator - the IntSupplier that returns byte values (0-255)
      Throws:
      IllegalArgumentException - if size is negative
      NullPointerException - if generator is null
  • Method Details

    • withRandomBytes

      public static DataGeneratorInputStream withRandomBytes(long size)
      Creates a stream that generates random bytes in the range 0-255 using the default seed (12345L).
      Parameters:
      size - the number of bytes to generate
      Returns:
      a DataGeneratorInputStream that generates random bytes
    • withRandomBytes

      public static DataGeneratorInputStream withRandomBytes(long size, long seed)
      Creates a stream that generates random bytes in the range 0-255 using the specified seed.
      Parameters:
      size - the number of bytes to generate
      seed - the seed for the random number generator
      Returns:
      a DataGeneratorInputStream that generates random bytes
    • withRandomBytes

      public static DataGeneratorInputStream withRandomBytes(long size, long seed, boolean includeZero)
      Creates a stream that generates random bytes using the specified seed.
      Parameters:
      size - the number of bytes to generate
      seed - the seed for the random number generator
      includeZero - if true, generates bytes 0-255; if false, generates bytes 1-255
      Returns:
      a DataGeneratorInputStream that generates random bytes
    • withRepeatingPattern

      public static DataGeneratorInputStream withRepeatingPattern(long size, String pattern)
      Creates a stream that repeatedly outputs the bytes from the given string (UTF-8 encoded).
      Parameters:
      size - the number of bytes to generate
      pattern - the string pattern to repeat
      Returns:
      a DataGeneratorInputStream that repeats the pattern
      Throws:
      NullPointerException - if pattern is null
      IllegalArgumentException - if pattern is empty
    • withRepeatingPattern

      public static DataGeneratorInputStream withRepeatingPattern(long size, byte[] pattern)
      Creates a stream that repeatedly outputs the bytes from the given byte array.
      Parameters:
      size - the number of bytes to generate
      pattern - the byte pattern to repeat
      Returns:
      a DataGeneratorInputStream that repeats the pattern
      Throws:
      NullPointerException - if pattern is null
      IllegalArgumentException - if pattern is empty
    • withConstantByte

      public static DataGeneratorInputStream withConstantByte(long size, int constantByte)
      Creates a stream that outputs the same byte value repeatedly.
      Parameters:
      size - the number of bytes to generate
      constantByte - the byte value to repeat (0-255)
      Returns:
      a DataGeneratorInputStream that outputs the constant byte
    • withSequentialBytes

      public static DataGeneratorInputStream withSequentialBytes(long size, int startByte, int endByte)
      Creates a stream that counts sequentially between two byte values, wrapping when reaching the end. If startByte <= endByte counts upward. If startByte > endByte counts downward.

      Examples:

      • withSequentialBytes(size, 10, 20) generates: 10, 11, 12, ..., 20, 10, 11, ...
      • withSequentialBytes(size, 20, 10) generates: 20, 19, 18, ..., 10, 20, 19, ...
      Parameters:
      size - the number of bytes to generate
      startByte - the starting byte value (0-255)
      endByte - the ending byte value (0-255)
      Returns:
      a DataGeneratorInputStream that counts sequentially
    • withRandomStrings

      public static DataGeneratorInputStream withRandomStrings(long size, Random random, int minWordLen, int maxWordLen, int separator)
      Creates a stream that generates random proper-case alphabetic strings (like "Xkqmz Pqwer Fgthn") using StringUtilities.getRandomString(Random, int, int). Each generated string has its first character uppercase and remaining characters lowercase. Strings are separated by the specified separator byte.
      Parameters:
      size - the number of bytes to generate
      random - the Random instance to use
      minWordLen - minimum length of each generated string (inclusive)
      maxWordLen - maximum length of each generated string (inclusive)
      separator - the byte to place between strings (e.g., ' ' or '\n')
      Returns:
      a DataGeneratorInputStream that generates random strings
      Throws:
      NullPointerException - if random is null
    • withGenerator

      public static DataGeneratorInputStream withGenerator(long size, IntSupplier generator)
      Creates a stream that uses a custom IntSupplier for generating bytes. The IntSupplier should return values in the range 0-255 representing byte values.
      Parameters:
      size - the number of bytes to generate
      generator - the IntSupplier that returns byte values (0-255)
      Returns:
      a DataGeneratorInputStream that uses the custom generator
      Throws:
      NullPointerException - if generator is null
    • read

      public int read()
      Reads the next byte of data from this input stream.
      Specified by:
      read in class InputStream
      Returns:
      the next byte of data (0-255), or -1 if the end of the stream is reached
    • read

      public int read(byte[] b, int off, int len)
      Reads up to len bytes of data from this input stream into an array of bytes. This method is more efficient than reading byte-by-byte.
      Overrides:
      read in class InputStream
      Parameters:
      b - the buffer into which the data is read
      off - the start offset in array b at which the data is written
      len - the maximum number of bytes to read
      Returns:
      the total number of bytes read into the buffer, or -1 if EOF
      Throws:
      NullPointerException - if b is null
      IndexOutOfBoundsException - if off or len are invalid
    • available

      public int available()
      Returns an estimate of the number of bytes that can be read from this input stream without blocking.
      Overrides:
      available in class InputStream
      Returns:
      the number of bytes remaining in this stream
    • skip

      public long skip(long n)
      Skips over and discards n bytes of data from this input stream. Note: The generator's getAsInt() method is still called for each skipped byte to maintain consistency with the generation sequence.
      Overrides:
      skip in class InputStream
      Parameters:
      n - the number of bytes to skip
      Returns:
      the actual number of bytes skipped