aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/net/DatagramPacket.java
blob: d2abb7fa9a26619fffb3d5b2b6617b1ba21a8d6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// DatagramPacket.java - Represents packets in a connectionless protocol.

/* Copyright (C) 1999  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.net;

/**
 * @author Warren Levy <warrenl@cygnus.com>
 * @date April 28, 1999.
 */

/**
 * Written using on-line Java Platform 1.2 API Specification, as well
 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
 * Status:  Believed complete and correct.
 */

public final class DatagramPacket
{
  private byte[] buffer;
  private int offset;
  private int length;
  private InetAddress address;
  private int port;

  // JDK1.2
  public DatagramPacket(byte[] buf, int offset, int length)
  {
    // FIXME: We can't currently rely on NullPointerException being
    // thrown when we invoke a method on a null object.
    if (buf == null)
      throw new NullPointerException("Null buffer");
    if (offset < 0)
      throw new IllegalArgumentException("Invalid offset: " + offset);
    if (length < 0)
      throw new IllegalArgumentException("Invalid length: " + length);
    if (offset + length > buf.length)
      throw new IllegalArgumentException("Potential buffer overflow - offset: "
			+ offset + " length: " + length);

    buffer = buf;
    this.offset = offset;
    this.length = length;
    this.address = null;
    this.port = -1;
  }

  public DatagramPacket(byte[] buf, int length)
  {
    this(buf, 0, length);
  }

  // JDK1.2
  public DatagramPacket(byte[] buf, int offset, int length,
	InetAddress address, int port)
  {
    // FIXME: We can't currently rely on NullPointerException being
    // thrown when we invoke a method on a null object.
    if (buf == null)
      throw new NullPointerException("Null buffer");
    if (offset < 0)
      throw new IllegalArgumentException("Invalid offset: " + offset);
    if (length < 0)
      throw new IllegalArgumentException("Invalid length: " + length);
    if (offset + length > buf.length)
      throw new IllegalArgumentException("Potential buffer overflow - offset: "
			+ offset + " length: " + length);
    if (port < 0 || port > 65535)
      throw new IllegalArgumentException("Invalid port: " + port);
    if (address == null)
      throw new NullPointerException("Null address");

    buffer = buf;
    this.offset = offset;
    this.length = length;
    this.address = address;
    this.port = port;
  }

  public DatagramPacket(byte[] buf, int length, InetAddress address, int port)
  {
    this(buf, 0, length, address, port);
  }

  public synchronized InetAddress getAddress()
  {
    return address;
  }

  public synchronized int getPort()
  {
    return port;
  }

  public synchronized byte[] getData()
  {
    return buffer;
  }

  // JDK1.2
  public synchronized int getOffset()
  {
    return offset;
  }

  public synchronized int getLength()
  {
    return length;
  }

  public synchronized void setAddress(InetAddress iaddr)
  {
    if (iaddr == null)
      throw new NullPointerException("Null address");

    address = iaddr;
  }

  public synchronized void setPort(int iport)
  {
    if (iport < 0 || iport > 65535)
      throw new IllegalArgumentException("Invalid port: " + iport);

    port = iport;
  }

  public synchronized void setData(byte[] buf)
  {
    // This form of setData requires setLength to be called separately
    // and subsequently.
    if (buf == null)
      throw new NullPointerException("Null buffer");

    buffer = buf;
  }

  // JDK1.2
  public synchronized void setData(byte[] buf, int offset, int length)
  {
    // This form of setData must be used if offset is to be changed.

    // FIXME: We can't currently rely on NullPointerException being
    // thrown when we invoke a method on a null object.
    if (buf == null)
      throw new NullPointerException("Null buffer");
    if (offset < 0)
      throw new IllegalArgumentException("Invalid offset: " + offset);
    if (length < 0)
      throw new IllegalArgumentException("Invalid length: " + length);
    if (offset + length > buf.length)
      throw new IllegalArgumentException("Potential buffer overflow - offset: "
			+ offset + " length: " + length);

    buffer = buf;
    this.offset = offset;
    this.length = length;
  }

  public synchronized void setLength(int length)
  {
    if (length < 0)
      throw new IllegalArgumentException("Invalid length: " + length);
    if (offset + length > buffer.length)
      throw new IllegalArgumentException("Potential buffer overflow - offset: "
			+ offset + " length: " + length);

    this.length = length;
  }
}