Converting a ByteArrayInputStream to a ReadableByteChannel

Main.java:

import java.nio.channels.*;
import java.nio.*;
import java.io.*;
 
public class Main
{
   public static void main(String []args) throws Exception {
      byte[] buffer = "contents of the buffer".getBytes();
      ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
 
      ReadableByteChannel byteChannel = Channels.newChannel(bais);   
      ByteBuffer bb = ByteBuffer.allocate(10);
      
      int n;
      while ((n = byteChannel.read(bb)) > -1) {
         bb.rewind();
         
         for (int i=0; i<n; i++) {
            System.out.print((char) bb.get());
         }
      }
   }
}

Using an ArrayBlockingQueue

This example provides an example of all the significant methods in ArrayBlockingQueue.

Main.java:

import static java.lang.Math.*;
import java.util.concurrent.*;
import java.util.*;
  
public class Main
{
   public static void main(String []args) {
      BlockingQueue<Message> blockingQueue = new ArrayBlockingQueue<Message>(10);
  
      // put 10 elements on the queue
      Random r = new Random();
      for (int i=0; i<10; i++) {
         // abs() can be used without qualification because java.lang.Math is statically imported
         Message<String> m = new Message<String>("Random Message #" + abs(r.nextInt() % 100));
         System.out.println("Adding message '" + m + "'");
         try {
            blockingQueue.put(m);
         }
         catch(InterruptedException e) { } 
      }
       
      // removed 5 first elements and put them into a TreeSet, 
      // notice that the class Message implements Comparable to do so!
      System.out.println("nTreeSet");
      System.out.println("-------");
      TreeSet<Message> treeSet = new TreeSet<Message>();
      blockingQueue.drainTo(treeSet, 5);
      System.out.println(treeSet);
  
      // offer 6 elements to the (tail of the) queue, last element will not be inserted because 
      // queue is full and space does not free up within 5 seconds
      System.out.println("nOffer");
      System.out.println("-----");
      for (int i=0; i<6; i++) {
         Message<String> m = new Message<String>("Message #" + (i+1));
         System.out.println("Offering " + m);
         try {
            blockingQueue.offer(m, 5, TimeUnit.SECONDS);
         }
         catch(InterruptedException e) { }
      }
       
      // iterate over the elements left in the BlockingQueue
      System.out.println("nIterator");
      System.out.println("--------");
      Iterator<Message> iter = blockingQueue.iterator();
      while (iter.hasNext()) {
         System.out.println(iter.next());
      }
       
      System.out.println("nPeek (retrieve but not remove)");
      System.out.println("----");
      System.out.println(blockingQueue.peek());
  
      System.out.println("nPoll (retrieve and remove)");
      System.out.println("----");
      System.out.println("Removing " + blockingQueue.poll());
  
      System.out.println("nSize and Remaining capacity");
      System.out.println("-----------------");
      System.out.println("Size of queue: " + blockingQueue.size());
      System.out.println("Remaining capacity of queue: " + blockingQueue.remainingCapacity());
  
      System.out.println("nConverting to array Message[]");
      System.out.println("-----------------------------");
      Message[] messages = blockingQueue.toArray(new Message[] { });
      for (int i=0; i<messages.length; i++) {
         System.out.println("From array: " + messages[i]);
      }
   }
}
  
class Message<T extends Comparable> implements Comparable<T>
{
   private T contents;
    
   public Message(T contents) {
      this.contents = contents;
   }
    
   public T getContents() {
      return contents;
   }
    
   public String toString() {
      return ""+contents;
   }
    
   public int compareTo(T o) {
      return o.compareTo(contents);
   }   
}

outputs:

Adding message 'Random Message #68'
Adding message 'Random Message #53'
Adding message 'Random Message #27'
Adding message 'Random Message #35'
Adding message 'Random Message #40'
Adding message 'Random Message #14'
Adding message 'Random Message #61'
Adding message 'Random Message #50'
Adding message 'Random Message #81'
Adding message 'Random Message #5'
  
TreeSet
-------
[Random Message #27, Random Message #35, Random Message #40, Random Message #53,
 Random Message #68]
  
Offer
-----
Offering Message #1
Offering Message #2
Offering Message #3
Offering Message #4
Offering Message #5
Offering Message #6
  
Iterator
--------
Random Message #14
Random Message #61
Random Message #50
Random Message #81
Random Message #5
Message #1
Message #2
Message #3
Message #4
Message #5
  
Peek (retrieve but not remove)
----
Random Message #14
  
Poll (retrieve and remove)
----
Removing Random Message #14
  
Size and Remaining capacity
-----------------
Size of queue: 9
Remaining capacity of queue: 1
  
Converting to array Message[]
-----------------------------
From array: Random Message #61
From array: Random Message #50
From array: Random Message #81
From array: Random Message #5
From array: Message #1
From array: Message #2
From array: Message #3
From array: Message #4
From array: Message #5