Package com.cedarsoftware.util
Class DataGeneratorInputStream
java.lang.Object
java.io.InputStream
com.cedarsoftware.util.DataGeneratorInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
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 Summary
ConstructorsConstructorDescriptionDataGeneratorInputStream(long size, IntSupplier generator) Creates a DataGeneratorInputStream with the specified size and generator. -
Method Summary
Modifier and TypeMethodDescriptionintReturns an estimate of the number of bytes that can be read from this input stream without blocking.intread()Reads the next byte of data from this input stream.intread(byte[] b, int off, int len) Reads up to len bytes of data from this input stream into an array of bytes.longskip(long n) Skips over and discards n bytes of data from this input stream.static DataGeneratorInputStreamwithConstantByte(long size, int constantByte) Creates a stream that outputs the same byte value repeatedly.static DataGeneratorInputStreamwithGenerator(long size, IntSupplier generator) Creates a stream that uses a custom IntSupplier for generating bytes.static DataGeneratorInputStreamwithRandomBytes(long size) Creates a stream that generates random bytes in the range 0-255 using the default seed (12345L).static DataGeneratorInputStreamwithRandomBytes(long size, long seed) Creates a stream that generates random bytes in the range 0-255 using the specified seed.static DataGeneratorInputStreamwithRandomBytes(long size, long seed, boolean includeZero) Creates a stream that generates random bytes using the specified seed.static DataGeneratorInputStreamwithRandomStrings(long size, Random random, int minWordLen, int maxWordLen, int separator) Creates a stream that generates random proper-case alphabetic strings (like "Xkqmz Pqwer Fgthn") usingStringUtilities.getRandomString(Random, int, int).static DataGeneratorInputStreamwithRepeatingPattern(long size, byte[] pattern) Creates a stream that repeatedly outputs the bytes from the given byte array.static DataGeneratorInputStreamwithRepeatingPattern(long size, String pattern) Creates a stream that repeatedly outputs the bytes from the given string (UTF-8 encoded).static DataGeneratorInputStreamwithSequentialBytes(long size, int startByte, int endByte) Creates a stream that counts sequentially between two byte values, wrapping when reaching the end.Methods inherited from class java.io.InputStream
close, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skipNBytes, transferTo
-
Constructor Details
-
DataGeneratorInputStream
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 negativeNullPointerException- if generator is null
-
-
Method Details
-
withRandomBytes
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
Creates a stream that generates random bytes in the range 0-255 using the specified seed.- Parameters:
size- the number of bytes to generateseed- the seed for the random number generator- Returns:
- a DataGeneratorInputStream that generates random bytes
-
withRandomBytes
Creates a stream that generates random bytes using the specified seed.- Parameters:
size- the number of bytes to generateseed- the seed for the random number generatorincludeZero- if true, generates bytes 0-255; if false, generates bytes 1-255- Returns:
- a DataGeneratorInputStream that generates random bytes
-
withRepeatingPattern
Creates a stream that repeatedly outputs the bytes from the given string (UTF-8 encoded).- Parameters:
size- the number of bytes to generatepattern- the string pattern to repeat- Returns:
- a DataGeneratorInputStream that repeats the pattern
- Throws:
NullPointerException- if pattern is nullIllegalArgumentException- if pattern is empty
-
withRepeatingPattern
Creates a stream that repeatedly outputs the bytes from the given byte array.- Parameters:
size- the number of bytes to generatepattern- the byte pattern to repeat- Returns:
- a DataGeneratorInputStream that repeats the pattern
- Throws:
NullPointerException- if pattern is nullIllegalArgumentException- if pattern is empty
-
withConstantByte
Creates a stream that outputs the same byte value repeatedly.- Parameters:
size- the number of bytes to generateconstantByte- the byte value to repeat (0-255)- Returns:
- a DataGeneratorInputStream that outputs the constant byte
-
withSequentialBytes
Creates a stream that counts sequentially between two byte values, wrapping when reaching the end. IfstartByte <= endBytecounts upward. IfstartByte > endBytecounts 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 generatestartByte- 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") usingStringUtilities.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 generaterandom- the Random instance to useminWordLen- 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
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 generategenerator- 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:
readin classInputStream- 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:
readin classInputStream- Parameters:
b- the buffer into which the data is readoff- the start offset in array b at which the data is writtenlen- 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 nullIndexOutOfBoundsException- 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:
availablein classInputStream- 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:
skipin classInputStream- Parameters:
n- the number of bytes to skip- Returns:
- the actual number of bytes skipped
-