001: /*
002: * Copyright (c) 2001 by Matt Welsh and The Regents of the University of
003: * California. All rights reserved.
004: *
005: * Permission to use, copy, modify, and distribute this software and its
006: * documentation for any purpose, without fee, and without written agreement is
007: * hereby granted, provided that the above copyright notice and the following
008: * two paragraphs appear in all copies of this software.
009: *
010: * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
011: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
012: * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
013: * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
014: *
015: * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
016: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
017: * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
018: * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
019: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
020: *
021: * Author: Matt Welsh <mdw@cs.berkeley.edu>
022: *
023: */
024:
025: package seda.sandStorm.core;
026:
027: import seda.sandStorm.api.*;
028:
029: /**
030: * A BufferElement is a QueueElementIF which represents a memory buffer.
031: *
032: * @author Matt Welsh
033: */
034: public class BufferElement implements QueueElementIF {
035:
036: /**
037: * The data associated with this BufferElement.
038: */
039: public byte data[];
040:
041: /**
042: * The completion queue associated with this buffer.
043: */
044: public SinkIF compQ;
045:
046: /**
047: * A user-defined tag object associated with this buffer.
048: * Can be used as a back-pointer from the buffer to application
049: * state, e.g., for handling completions.
050: */
051: public Object userTag;
052:
053: /**
054: * The size of the data associated with this BufferElement. May not be
055: * equal to data.length; may be any value less than or equal to
056: * (data.length - offset).
057: */
058: public int size;
059:
060: /**
061: * The offet into the data associated with this BufferElement.
062: */
063: public int offset;
064:
065: /**
066: * Create a BufferElement with the given data, an offset of 0, and a
067: * size of data.length.
068: */
069: public BufferElement(byte data[]) {
070: this (data, 0, data.length, null);
071: }
072:
073: /**
074: * Create a BufferElement with the given data, an offset of 0, and a
075: * size of data.length, with the given completion queue.
076: */
077: public BufferElement(byte data[], SinkIF compQ) {
078: this (data, 0, data.length, compQ);
079: }
080:
081: /**
082: * Create a BufferElement with the given data, offset, and size.
083: */
084: public BufferElement(byte data[], int offset, int size) {
085: this (data, offset, size, null);
086: }
087:
088: /**
089: * Create a BufferElement with the given data, offset, size, and
090: * completion queue.
091: */
092: public BufferElement(byte data[], int offset, int size, SinkIF compQ) {
093: this .data = data;
094: if ((offset >= data.length) || (size > (data.length - offset))) {
095: throw new IllegalArgumentException(
096: "BufferElement created with invalid offset and/or size (off="
097: + offset + ", size=" + size
098: + ", data.length=" + data.length + ")");
099: }
100: this .offset = offset;
101: this .size = size;
102: this .compQ = compQ;
103: }
104:
105: /**
106: * Create a BufferElement with a new data array of the given size.
107: */
108: public BufferElement(int size) {
109: this (new byte[size], 0, size, null);
110: }
111:
112: /**
113: * Return the data.
114: */
115: public byte[] getBytes() {
116: return data;
117: }
118:
119: /**
120: * Return the size.
121: */
122: public int getSize() {
123: return size;
124: }
125:
126: /**
127: * Return the offset.
128: */
129: public int getOffset() {
130: return offset;
131: }
132:
133: /**
134: * Return the completion queue for this buffer.
135: */
136: public SinkIF getCompletionQueue() {
137: return compQ;
138: }
139:
140: }
|