001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.net;
019:
020: import org.apache.harmony.luni.util.Msg;
021:
022: /**
023: * This class models a datagram packet to be sent or received. The
024: * DatagramPacket(byte[], int, InetAddress, int) constructor is used for packets
025: * to be sent, while the DatagramPacket(byte[], int) constructor is used for
026: * received packets.
027: *
028: * @see DatagramSocket
029: */
030: public final class DatagramPacket {
031:
032: byte[] data;
033:
034: /**
035: * Length of the data to be sent or size of data that was received via
036: * DatagramSocket#receive() method call.
037: */
038: int length;
039:
040: /**
041: * Size of internal buffer that is used to store received data. Should be
042: * greater or equal to "length" field.
043: */
044: int capacity;
045:
046: InetAddress address;
047:
048: int port = -1; // The default port number is -1
049:
050: int offset = 0;
051:
052: /**
053: * Constructs a new <code>DatagramPacket</code> suitable for receiving
054: * datagram packets of length up to <code>length</code>.
055: *
056: * @param data
057: * byte array to store the read characters
058: * @param length
059: * length of the data buffer
060: */
061: public DatagramPacket(byte[] data, int length) {
062: this (data, 0, length);
063: }
064:
065: /**
066: * Constructs a new <code>DatagramPacket</code> suitable for receiving
067: * datagram packets of length up to <code>length</code>, with an offset
068: * into the buffer <code>offset</code>.
069: *
070: * @param data
071: * byte array to store the read characters
072: * @param offset
073: * the offset into the byte array
074: * @param length
075: * length of the data buffer
076: */
077: public DatagramPacket(byte[] data, int offset, int length) {
078: super ();
079: setData(data, offset, length);
080: }
081:
082: /**
083: * Constructs a new <code>DatagramPacket</code> suitable for sending
084: * packets to the nominated host/port. The <code>length</code> must be
085: * less than or equal to the size of <code>data</code>.
086: *
087: * @param data
088: * byte array to store the read characters
089: * @param offset
090: * the offset in to read/write from
091: * @param length
092: * length of the data buffer
093: * @param host
094: * address of the target host
095: * @param aPort
096: * target host port
097: */
098:
099: public DatagramPacket(byte[] data, int offset, int length,
100: InetAddress host, int aPort) {
101: this (data, offset, length);
102: setPort(aPort);
103: address = host;
104: }
105:
106: /**
107: * Constructs a new <code>DatagramPacket</code> suitable for sending
108: * packets to the nominated host/port. The <code>length</code> must be
109: * less than or equal to the size of <code>data</code>.
110: *
111: * @param data
112: * byte array to store the read characters
113: * @param length
114: * length of the data buffer
115: * @param host
116: * address of the target host
117: * @param port
118: * target host port
119: */
120: public DatagramPacket(byte[] data, int length, InetAddress host,
121: int port) {
122: this (data, 0, length, host, port);
123: }
124:
125: /**
126: * Answer the IP address of the machine that is the target or sender of this
127: * datagram.
128: *
129: * @return InetAddress the target host address
130: */
131: public synchronized InetAddress getAddress() {
132: return address;
133: }
134:
135: /**
136: * Answer the data sent or received in this datagram.
137: *
138: * @return byte[] the data sent/received
139: */
140: public synchronized byte[] getData() {
141: return data;
142: }
143:
144: /**
145: * Answer the length of the data sent or received in this datagram.
146: *
147: * @return int the length of the sent/received data
148: */
149: public synchronized int getLength() {
150: return length;
151: }
152:
153: /**
154: * Answer the offset of the data sent or received in this datagram buffer.
155: *
156: * @return int the offset of the start of the sent/received data
157: */
158: public synchronized int getOffset() {
159: return offset;
160: }
161:
162: /**
163: * Answer the port number of the target or sender machine of this datagram.
164: *
165: * @return int for received packets, the sender address and for sent
166: * packets, the target host
167: */
168: public synchronized int getPort() {
169: return port;
170: }
171:
172: /**
173: * Set the IP address of the machine that is the target of this datagram.
174: *
175: * @param addr
176: * the target host address
177: */
178: public synchronized void setAddress(InetAddress addr) {
179: address = addr;
180: }
181:
182: /**
183: * Set the data buffer for this datagram.
184: *
185: * @param buf
186: * the data to be sent
187: * @param anOffset
188: * the offset into the data
189: * @param aLength
190: * the length of the data to be sent
191: */
192: public synchronized void setData(byte[] buf, int anOffset,
193: int aLength) {
194: if (0 > anOffset || anOffset > buf.length || 0 > aLength
195: || aLength > buf.length - anOffset) {
196: throw new IllegalArgumentException(Msg.getString("K002f")); //$NON-NLS-1$
197: }
198: data = buf;
199: offset = anOffset;
200: length = aLength;
201: capacity = aLength;
202: }
203:
204: /**
205: * Set the data sent in this datagram.
206: *
207: * @param buf
208: * the data to be sent
209: */
210: public synchronized void setData(byte[] buf) {
211: length = buf.length; // This will check for null
212: capacity = buf.length;
213: data = buf;
214: offset = 0;
215: }
216:
217: /**
218: * Set the length of the data sent in this datagram.
219: *
220: * @param len
221: * the length of the data to be sent
222: */
223: public synchronized void setLength(int len) {
224: if (0 > len || offset + len > data.length) {
225: throw new IllegalArgumentException(Msg.getString("K002f")); //$NON-NLS-1$
226: }
227: length = len;
228: capacity = len;
229: }
230:
231: /**
232: * Set the port number of the target machine of this datagram.
233: *
234: * @param aPort
235: * the target host port
236: */
237: public synchronized void setPort(int aPort) {
238: if (aPort < 0 || aPort > 65535) {
239: throw new IllegalArgumentException(Msg.getString(
240: "K0325", aPort)); //$NON-NLS-1$
241: }
242: port = aPort;
243: }
244:
245: /**
246: * Constructs a new <code>DatagramPacket</code> suitable for sending
247: * packets to the nominated host/port. The <code>length</code> must be
248: * less than or equal to the size of <code>data</code>.
249: *
250: * @param data
251: * byte array to store the read characters
252: * @param length
253: * length of the data buffer
254: * @param sockAddr
255: * the machine address and port
256: */
257: public DatagramPacket(byte[] data, int length,
258: SocketAddress sockAddr) throws SocketException {
259: this (data, 0, length);
260: setSocketAddress(sockAddr);
261: }
262:
263: /**
264: * Constructs a new <code>DatagramPacket</code> suitable for sending
265: * packets to the nominated host/port. The <code>length</code> must be
266: * less than or equal to the size of <code>data</code>.
267: *
268: * @param data
269: * byte array to store the read characters
270: * @param offset
271: * the offset in to read/write from
272: * @param length
273: * length of the data buffer
274: * @param sockAddr
275: * the machine address and port
276: */
277: public DatagramPacket(byte[] data, int offset, int length,
278: SocketAddress sockAddr) throws SocketException {
279: this (data, offset, length);
280: setSocketAddress(sockAddr);
281: }
282:
283: /**
284: * Answer the SocketAddress for this packet.
285: */
286: public synchronized SocketAddress getSocketAddress() {
287: return new InetSocketAddress(getAddress(), getPort());
288: }
289:
290: /**
291: * Set the SocketAddress for this packet.
292: *
293: * @param sockAddr
294: * the machine address and port
295: */
296: public synchronized void setSocketAddress(SocketAddress sockAddr) {
297: if (!(sockAddr instanceof InetSocketAddress)) {
298: throw new IllegalArgumentException(
299: Msg
300: .getString(
301: "K0316", sockAddr == null ? null : sockAddr.getClass())); //$NON-NLS-1$
302: }
303: InetSocketAddress inetAddr = (InetSocketAddress) sockAddr;
304: port = inetAddr.getPort();
305: address = inetAddr.getAddress();
306: }
307: }
|