Last active
October 31, 2024 10:36
-
-
Save justjanne/12306b797f4faa977436070ec0bd865f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileDescriptor; | |
import java.io.IOException; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.FileChannel; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.Paths; | |
import java.nio.file.StandardOpenOption; | |
public class Jes { | |
/** | |
* Maximum buffer size to be allocated | |
*/ | |
private static final int BUFFER_SIZE = 8192; | |
/** | |
* Default value to be used if none given | |
*/ | |
private static final String DEFAULT_VALUE = "y"; | |
/** | |
* Whether the test should be realistic | |
* (the buffer should be cleared and refilled every time) | |
*/ | |
private static final boolean REALISTIC_TEST = true; | |
public static void main(String... args) throws IOException { | |
ByteBuffer template = StandardCharsets.UTF_8.encode(getArgument(DEFAULT_VALUE, args)); | |
ByteBuffer buffer = getBuffer(BUFFER_SIZE, template); | |
FileChannel open = FileChannel.open(Paths.get("/proc/self/fd/1"), | |
StandardOpenOption.APPEND, StandardOpenOption.WRITE); | |
while (true) { | |
buffer.clear(); | |
if (REALISTIC_TEST) { | |
fillBuffer(buffer, template); | |
} | |
open.write(buffer); | |
} | |
} | |
/** | |
* Fills the given buffer repeatedly with the given template | |
* | |
* @param buffer Buffer to fill with given template | |
* @param template Template to fill buffer with | |
*/ | |
private static void fillBuffer(ByteBuffer buffer, ByteBuffer template) { | |
int templateLength = template.limit(); | |
int amount = buffer.limit() / templateLength; | |
for (int i = 0; i < amount; i++) { | |
for (int j = 0; j < templateLength; j++) { | |
buffer.put(i * templateLength + j, template.get(j)); | |
} | |
} | |
} | |
/** | |
* Builds a new native bytebuffer exactly fitting for the given max length and template | |
* The buffer will have the size of the largest integer multiple of the template's | |
* length that is smaller than or equal to the given maxLength. | |
* | |
* @param maxLength Maximum desired length for the buffer | |
* @param template Template to fill the buffer with | |
*/ | |
private static ByteBuffer getBuffer(int maxLength, ByteBuffer template) { | |
int templateLength = template.limit(); | |
int amount = maxLength / templateLength; | |
return ByteBuffer.allocateDirect(amount * templateLength); | |
} | |
/** | |
* Builds the template string that should be repeated | |
* | |
* @param defValue Default value to be used if no arguments are given | |
* @param args Command line arguments given to the program | |
* | |
* @return A string containing all command line arguments, or, if none given, the default value. | |
*/ | |
private static String getArgument(String defValue, String[] args) { | |
if (args.length > 0) { | |
StringBuilder builder = new StringBuilder(); | |
for (String arg : args) { | |
builder.append(arg); | |
} | |
builder.append("\n"); | |
return builder.toString(); | |
} else { | |
return defValue + "\n"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import sun.nio.ch.FileChannelImpl;
With the release of JDK9, this import will not work, as you won't be able to accesssun.*
for some stupid reason.