Java Stuff - fordsfords/fordsfords.github.io GitHub Wiki
I use Java very rarely, and my memory isn't great. So instead of googling 30 different things, I'll just dump a lot of info here.
Doc
[https://docs.oracle.com/javase/8/docs/api/index-files/index-1.html Oracle doc index for Java 8.]
- [https://docs.oracle.com/javase/8/docs/api/java/lang/String.html String]
- [https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html ByteBuffer]
length() vs. length
- array.length is a property (not method) of an array that tells how many elements it has.
- string.length() is a method of a string that tells how many characters it has.
ByteBuffer
Mostly from https://howtodoinjava.com/java/nio/java-nio-2-0-working-with-buffers/
A ByteBuffer is an object that basically holds four integers (for state) and a reference to something else that holds the actual data. Don't think of the byte buffer as actually having the data. A "direct" byte buffer references data outside of the JVM, like from JNI. Alternatively, it can be backed by a byte array.
- write data to it
- call buffer.flip()
- read data from it
- call buffer.clear
The four state integers (and their initial values upon creation):
- capacity = C : number of bytes in the underlying buffer.
- limit = C : count of "live" elements in the buffer.
- position = 0 : next position to read/write.
- mark = x : remembered position.
Calling put will write at [position] and increment position. Writing is done at "position". But you can only go as far as limit, which is fine since it starts out at capacity.
flip() indicates that we're done writing:
limit = position;
position = 0;
Now you can "get()", which reads from position, till limit is reached.
- hasRemaining() [true..false] : more data to read.
- remaining() [0 .. limit]: how much data is remaining to be read.
Don't flip it again! That doesn't prepare it for reading. Use clear()?
limit = capacity;
position = 0;
NIO vs Netty
Netty allows "direct" use of epoll. NIO apparently maybe has an epoll selector provider?