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: ByteBufferSource.java,v 1.7 2003/02/17 04:17:27 michaelh Exp $ 20 */ 21 22 package juju.reattore.io.impl; 23 24 import juju.reattore.io.*; 25 import java.nio.ByteBuffer; 26 27 /*** A thin wrapper around the ByteBuffer API that implements the 28 Reattore IO API. 29 */ 30 public class ByteBufferSource 31 implements ByteSource { 32 33 private ByteBuffer on; 34 private boolean fromPool; 35 36 /*** Create a new source wrapper around the given ByteBuffer. 37 38 @param on Buffer to wrap. 39 */ 40 public ByteBufferSource(ByteBuffer on) { 41 this.on = on; 42 } 43 44 /*** Create a new source containing the given array data. 45 46 @param wrap The data to contain. 47 */ 48 public ByteBufferSource(byte[] wrap) { 49 ByteBuffer on = ByteBufferPool.allocate(wrap.length); 50 fromPool = true; 51 52 on.put(wrap); 53 on.flip(); 54 55 setBackingBuffer(on); 56 } 57 58 /*** Internal constructor for derrivitaves that wish to set the 59 buffer later. 60 */ 61 protected ByteBufferSource() { 62 /* Do nothing */ 63 } 64 65 /*** Used by a derrivitave to set the backing buffer. 66 67 @param on The buffer to run against. 68 */ 69 protected void setBackingBuffer(ByteBuffer on) { 70 this.on = on; 71 } 72 73 /*** @see ByteSource */ 74 public int get() { 75 if (on.hasRemaining()) { 76 return on.get(); 77 } 78 else { 79 return EOF; 80 } 81 } 82 83 /*** @see ByteSource 84 */ 85 public int get(byte[] into, int offset, int length) { 86 int rem = on.remaining(); 87 int toGet = length > rem ? rem : length; 88 89 on.get(into, offset, toGet); 90 91 return toGet; 92 } 93 94 /*** @see ByteSource */ 95 public int remaining() { 96 return on.remaining(); 97 } 98 99 /*** Gives direct access to the backing buffer of this source for 100 copy operations. If this class is fresh then the buffer is 101 fresh, otherwise the state is undefined. 102 103 @return The backing buffer. 104 */ 105 public ByteBuffer getBackingBuffer() { 106 return on; 107 } 108 109 /*** @see Source */ 110 public void dispose() { 111 if (fromPool && on != null) { 112 ByteBufferPool.release(on); 113 on = null; 114 } 115 } 116 }

This page was automatically generated by Maven