OSDN Git Service

* java/lang/natClass.cc (_Jv_CheckCast): add class names to exception
[pf3gnuchains/gcc-fork.git] / libjava / java / net / DatagramPacket.java
1 // DatagramPacket.java - Represents packets in a connectionless protocol.
2
3 /* Copyright (C) 1999, 2000  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 package java.net;
12
13 /**
14  * @author Warren Levy <warrenl@cygnus.com>
15  * @date April 28, 1999.
16  */
17
18 /**
19  * Written using on-line Java Platform 1.2 API Specification, as well
20  * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
21  * Status:  Believed complete and correct.
22  */
23
24 public final class DatagramPacket
25 {
26   private byte[] buffer;
27   private int offset;
28   private int length;
29   private InetAddress address;
30   private int port;
31
32   // JDK1.2
33   public DatagramPacket(byte[] buf, int offset, int length)
34   {
35     if (buf == null)
36       throw new NullPointerException("Null buffer");
37     if (offset < 0)
38       throw new IllegalArgumentException("Invalid offset: " + offset);
39     if (length < 0)
40       throw new IllegalArgumentException("Invalid length: " + length);
41     if (offset + length > buf.length)
42       throw new IllegalArgumentException("Potential buffer overflow - offset: "
43                         + offset + " length: " + length);
44
45     buffer = buf;
46     this.offset = offset;
47     this.length = length;
48     this.address = null;
49     this.port = -1;
50   }
51
52   public DatagramPacket(byte[] buf, int length)
53   {
54     this(buf, 0, length);
55   }
56
57   // JDK1.2
58   public DatagramPacket(byte[] buf, int offset, int length,
59         InetAddress address, int port)
60   {
61     if (buf == null)
62       throw new NullPointerException("Null buffer");
63     if (offset < 0)
64       throw new IllegalArgumentException("Invalid offset: " + offset);
65     if (length < 0)
66       throw new IllegalArgumentException("Invalid length: " + length);
67     if (offset + length > buf.length)
68       throw new IllegalArgumentException("Potential buffer overflow - offset: "
69                         + offset + " length: " + length);
70     if (port < 0 || port > 65535)
71       throw new IllegalArgumentException("Invalid port: " + port);
72     if (address == null)
73       throw new NullPointerException("Null address");
74
75     buffer = buf;
76     this.offset = offset;
77     this.length = length;
78     this.address = address;
79     this.port = port;
80   }
81
82   public DatagramPacket(byte[] buf, int length, InetAddress address, int port)
83   {
84     this(buf, 0, length, address, port);
85   }
86
87   public synchronized InetAddress getAddress()
88   {
89     return address;
90   }
91
92   public synchronized int getPort()
93   {
94     return port;
95   }
96
97   public synchronized byte[] getData()
98   {
99     return buffer;
100   }
101
102   // JDK1.2
103   public synchronized int getOffset()
104   {
105     return offset;
106   }
107
108   public synchronized int getLength()
109   {
110     return length;
111   }
112
113   public synchronized void setAddress(InetAddress iaddr)
114   {
115     if (iaddr == null)
116       throw new NullPointerException("Null address");
117
118     address = iaddr;
119   }
120
121   public synchronized void setPort(int iport)
122   {
123     if (iport < 0 || iport > 65535)
124       throw new IllegalArgumentException("Invalid port: " + iport);
125
126     port = iport;
127   }
128
129   public synchronized void setData(byte[] buf)
130   {
131     // This form of setData requires setLength to be called separately
132     // and subsequently.
133     if (buf == null)
134       throw new NullPointerException("Null buffer");
135
136     buffer = buf;
137   }
138
139   // JDK1.2
140   public synchronized void setData(byte[] buf, int offset, int length)
141   {
142     // This form of setData must be used if offset is to be changed.
143
144     if (buf == null)
145       throw new NullPointerException("Null buffer");
146     if (offset < 0)
147       throw new IllegalArgumentException("Invalid offset: " + offset);
148     if (length < 0)
149       throw new IllegalArgumentException("Invalid length: " + length);
150     if (offset + length > buf.length)
151       throw new IllegalArgumentException("Potential buffer overflow - offset: "
152                         + offset + " length: " + length);
153
154     buffer = buf;
155     this.offset = offset;
156     this.length = length;
157   }
158
159   public synchronized void setLength(int length)
160   {
161     if (length < 0)
162       throw new IllegalArgumentException("Invalid length: " + length);
163     if (offset + length > buffer.length)
164       throw new IllegalArgumentException("Potential buffer overflow - offset: "
165                         + offset + " length: " + length);
166
167     this.length = length;
168   }
169 }