FileDocCategorySizeDatePackage
FibonacciConsumer.javaAPI DocExample1080Tue Feb 14 10:46:34 GMT 2006None

FibonacciConsumer

public class FibonacciConsumer extends Thread

Fields Summary
private ReadableByteChannel
in
Constructors Summary
public FibonacciConsumer(ReadableByteChannel in)

    this.in = in;
  
Methods Summary
public voidrun()


    ByteBuffer sizeb = ByteBuffer.allocate(4);
    try {
      while (sizeb.hasRemaining()) in.read(sizeb);
      sizeb.flip();
      int howMany = sizeb.getInt();
      sizeb.clear();
      
      for (int i = 0; i < howMany; i++) {
        while (sizeb.hasRemaining()) in.read(sizeb);
        sizeb.flip();
        int length = sizeb.getInt();
        sizeb.clear();
        
        ByteBuffer data = ByteBuffer.allocate(length);
        while (data.hasRemaining()) in.read(data);
        
        BigInteger result = new BigInteger(data.array());
        System.out.println(result);
      }
    }
    catch (IOException ex) {
      System.err.println(ex);
    }
    finally {
      try {
        in.close();
      } 
      catch (Exception ex) {
        // We tried
      } 
    }