View Javadoc
1 /* Reattore HTTP Server 2 3 Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz> 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 19 $Id: SourceHelper.java,v 1.8 2003/02/22 04:29:52 michaelh Exp $ 20 */ 21 22 package juju.reattore.io.impl; 23 24 import java.nio.ByteBuffer; 25 import java.io.IOException; 26 27 import org.apache.commons.logging.*; 28 29 import juju.reattore.io.*; 30 31 /*** Helper methods that transform sources. 32 */ 33 public class SourceHelper { 34 35 private static Log log = LogFactory.getLog(SourceHelper.class); 36 37 /*** Return a source as a NIO ByteBuffer. 38 39 @param in The source to wrap. 40 @return The source as a byte buffer. 41 @throws IOException on error. 42 43 @todo Minor: Uses byte buffers off the heap in some cases. 44 */ 45 public static ByteBuffer asByteBuffer(ByteSource in) 46 throws IOException { 47 48 if (in instanceof ByteBufferSource) { 49 return ((ByteBufferSource)in).getBackingBuffer(); 50 } 51 else if (in instanceof BulkByteSource) { 52 return ((BulkByteSource)in).getBulk(); 53 } 54 else { 55 log.info("Inefficently converting between sources."); 56 57 byte[] body = new byte[in.remaining()]; 58 in.get(body, 0, body.length); 59 60 ByteBuffer ret = ByteBuffer.allocate(body.length); 61 ret.put(body); 62 63 ret.flip(); 64 65 return ret; 66 } 67 } 68 }

This page was automatically generated by Maven