1 7 8 package org.jboss.media.util; 9 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.OutputStream ; 13 import java.nio.ByteBuffer ; 14 15 30 public class ByteBufferUtils 31 { 32 36 public static OutputStream newOutputStream(final ByteBuffer buf) 37 { 38 return new OutputStream () 39 { 40 public synchronized void write(int b) throws IOException 41 { 42 buf.put((byte) b); 43 } 44 45 public synchronized void write(byte[] bytes, int off, int len) 46 throws IOException 47 { 48 buf.put(bytes, off, len); 49 } 50 }; 51 } 52 53 57 public static InputStream newInputStream(final ByteBuffer buf) 58 { 59 return new InputStream () 60 { 61 public synchronized int read() throws IOException 62 { 63 if (!buf.hasRemaining()) 64 { 65 return -1; 66 } 67 return buf.get(); 68 } 69 70 public synchronized int read(byte[] bytes, int off, int len) 71 throws IOException 72 { 73 len = Math.min(len, buf.remaining()); 75 buf.get(bytes, off, len); 76 return len; 77 } 78 }; 79 } 80 } 81 | Popular Tags |