Proszę o wytłumaczenie działania metody read(CharBuffer cb). Nie rozumiem tego jak ona jest wywoływana i co się dzieje z obiektem cb, gdzie on dalej jest zwracany?
package interfaces;
import java.io.IOException;
import java.nio.CharBuffer;
import java.util.Random;
import java.util.Scanner;
class RandomChar{
private static Random rand = new Random();
public char next(){
return (char) rand.nextInt(127);
}
}
public class AdapterRandomChar extends RandomChar implements Readable{
private int count;
publicAdapterRandomChar(int count) {
this.count=count;
}
@Override
public int read(CharBuffer cb) throws IOException {
if(count--==0)
return -1;
cb.append(next()).append(" ");
return cb.toString().length();
}
public static void main(String[] args) {
Scanner sc = new Scanner(new AdapterRandomChar(5));
while(sc.hasNext()){
System.out.println(sc.next());
}
}
}