001: /*
002: * Copyright (c) xsocket.org, 2006 - 2008. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
019: * The latest copy of this software may be found on http://www.xsocket.org/
020: */
021: package org.xsocket.connection.spi;
022:
023: import java.nio.ByteBuffer;
024:
025: /**
026: *
027: * @author grro@xsocket.org
028: */
029: final class IoQueue {
030:
031: /*
032: * the implementation is driven by the assumption, that
033: * in most cases just one buffer or one buffer array
034: * will be enqueued
035: */
036:
037: private ByteBuffer[] buffers = null;
038:
039: /**
040: * returns true, if empty
041: *
042: * @return true, if empty
043: */
044: public synchronized boolean isEmpty() {
045: if (buffers == null) {
046: return true;
047:
048: } else {
049: return (getSize() == 0);
050: }
051: }
052:
053: /**
054: * return the current size
055: *
056: * @return the current size
057: */
058: public synchronized int getSize() {
059: int size = 0;
060:
061: if (buffers != null) {
062: for (int i = 0; i < buffers.length; i++) {
063: if (buffers[i] != null) {
064: size += buffers[i].remaining();
065: }
066: }
067: }
068:
069: return size;
070: }
071:
072: /**
073: * append a byte buffer to this queue.
074: *
075: * @param data the ByteBuffer to append
076: */
077: public synchronized void append(ByteBuffer data) {
078: if (data == null) {
079: return;
080: }
081:
082: if (buffers == null) {
083: buffers = new ByteBuffer[1];
084: buffers[0] = data;
085:
086: } else {
087: ByteBuffer[] newBuffers = new ByteBuffer[buffers.length + 1];
088: System.arraycopy(buffers, 0, newBuffers, 0, buffers.length);
089: newBuffers[buffers.length] = data;
090: buffers = newBuffers;
091: }
092: }
093:
094: /**
095: * append a list of byte buffer to this queue. By adding a list,
096: * the list becomes part of to the buffer, and should not be modified outside the buffer
097: * to avoid side effects
098: *
099: * @param bufs the list of ByteBuffer
100: */
101: public synchronized void append(ByteBuffer[] data) {
102: if (data == null) {
103: return;
104: }
105:
106: if (buffers == null) {
107: buffers = data;
108:
109: } else {
110: ByteBuffer[] newBuffers = new ByteBuffer[buffers.length
111: + data.length];
112: System.arraycopy(buffers, 0, newBuffers, 0, buffers.length);
113: System.arraycopy(data, 0, newBuffers, buffers.length,
114: data.length);
115: buffers = newBuffers;
116: }
117: }
118:
119: /**
120: * add the given ByteBuffer array into the head of the queue
121: *
122: * @param bufs the list of ByteBuffer
123: */
124: public synchronized void addFirst(ByteBuffer[] data) {
125: if (data == null) {
126: return;
127: }
128:
129: if (buffers == null) {
130: buffers = data;
131:
132: } else {
133: ByteBuffer[] newBuffers = new ByteBuffer[buffers.length
134: + data.length];
135: System.arraycopy(data, 0, newBuffers, 0, data.length);
136: System.arraycopy(buffers, 0, newBuffers, data.length,
137: buffers.length);
138: buffers = newBuffers;
139: }
140: }
141:
142: /**
143: * drain this queue
144: *
145: * @return the contained ByteBuffer array or <code>null</code>
146: */
147: public synchronized ByteBuffer[] drain() {
148: ByteBuffer[] result = buffers;
149: buffers = null;
150:
151: return result;
152: }
153: }
|