Java : Readable with Examples

Readable (Java SE 22 & JDK 22) with Examples.
You will find code examples on most Readable methods.


Summary

A Readable is a source of characters. Characters from a Readable are made available to callers of the read method via a CharBuffer.

Class diagram

final Readable readable = CharBuffer.wrap("abc");
final var cb = CharBuffer.allocate(5);

System.out.println(readable.read(cb)); // 3
System.out.println(Arrays.toString(cb.array())); // [a, b, c,  ,  ]

Methods

int read (CharBuffer cb)

Attempts to read characters into the specified character buffer.

final Readable readable = CharBuffer.wrap("abc");
final var cb = CharBuffer.allocate(5);

System.out.println(readable.read(cb)); // 3
System.out.println(Arrays.toString(cb.array())); // [a, b, c,  ,  ]
final Readable readable = CharBuffer.wrap("abcdXYZ");

final var cb1 = CharBuffer.allocate(5);

System.out.println(readable.read(cb1)); // 5
System.out.println(Arrays.toString(cb1.array())); // [a, b, c, d, X]

final var cb2 = CharBuffer.allocate(5);

System.out.println(readable.read(cb2)); // 2
System.out.println(Arrays.toString(cb2.array())); // [Y, Z,  ,  ,  ]

Related posts

To top of page