Skip to content

Instantly share code, notes, and snippets.

@justjanne
Last active October 31, 2024 10:36
Show Gist options
  • Save justjanne/12306b797f4faa977436070ec0bd865f to your computer and use it in GitHub Desktop.
Save justjanne/12306b797f4faa977436070ec0bd865f to your computer and use it in GitHub Desktop.
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";
}
}
}
@ArsenArsen
Copy link

ArsenArsen commented Jun 13, 2017

Line 44: value.getBytes("UTF-8")

Any reason for using the charset manially?

EDIT: Oh, to get the native buffer. nvm

@ArsenArsen
Copy link

import sun.nio.ch.FileChannelImpl; With the release of JDK9, this import will not work, as you won't be able to access sun.* for some stupid reason.

@justjanne
Copy link
Author

@ArsenArsen: Edited, now works also on JDK 9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment