37 |
|
|
38 |
package java.nio; |
package java.nio; |
39 |
|
|
|
|
|
40 |
public abstract class Buffer |
public abstract class Buffer |
41 |
{ |
{ |
42 |
int cap, limit, pos, mark; |
int cap = 0; |
43 |
|
int limit = 0; |
44 |
Buffer() |
int pos = 0; |
45 |
{ |
int mark = -1; |
46 |
} |
|
47 |
|
/** |
48 |
public final int capacity() |
* Retrieves the capacity of the buffer. |
49 |
{ |
*/ |
50 |
return cap; |
public final int capacity () |
51 |
} |
{ |
52 |
|
return cap; |
53 |
public final int capacity(int c) |
} |
54 |
{ |
|
55 |
int old = cap; |
/** |
56 |
cap = c; |
* Clears the buffer. |
57 |
return old; |
*/ |
58 |
} |
public final Buffer clear () |
59 |
|
{ |
60 |
public final Buffer clear() |
limit = cap; |
61 |
{ |
pos = 0; |
62 |
limit = cap; |
mark = -1; |
63 |
mark = 0; |
return this; |
64 |
pos = 0; |
} |
|
return this; |
|
|
} |
|
|
|
|
|
public final Buffer flip() |
|
|
{ |
|
|
limit = pos; |
|
|
pos = 0; |
|
|
|
|
|
mark = 0; |
|
|
|
|
|
return this; |
|
|
} |
|
|
|
|
|
public final boolean hasRemaining() |
|
|
{ |
|
|
return limit > pos; |
|
|
} |
|
|
|
|
|
public abstract boolean isReadOnly(); |
|
65 |
|
|
66 |
|
/** |
67 |
|
* Flips the buffer. |
68 |
|
*/ |
69 |
|
public final Buffer flip () |
70 |
|
{ |
71 |
|
limit = pos; |
72 |
|
pos = 0; |
73 |
|
mark = -1; |
74 |
|
return this; |
75 |
|
} |
76 |
|
|
77 |
public final int limit() |
/** |
78 |
{ |
* Tells whether the buffer has remaining data to read or not. |
79 |
return limit; |
*/ |
80 |
} |
public final boolean hasRemaining () |
81 |
|
{ |
82 |
public final Buffer limit(int newLimit) |
return limit > pos; |
83 |
{ |
} |
84 |
if (newLimit <= mark) |
|
85 |
mark = 0; |
/** |
86 |
|
* Tells whether this buffer is read only or not. |
87 |
if (pos > newLimit) |
*/ |
88 |
pos = newLimit - 1; |
public abstract boolean isReadOnly (); |
89 |
|
|
90 |
limit = newLimit; |
/** |
91 |
return this; |
* Retrieves the current limit of the buffer. |
92 |
} |
*/ |
93 |
|
public final int limit () |
94 |
public final Buffer mark() |
{ |
95 |
{ |
return limit; |
96 |
mark = pos; |
} |
97 |
return this; |
|
98 |
} |
/** |
99 |
|
* Sets this buffer's limit. |
100 |
public final int position() |
* |
101 |
{ |
* @param newLimit The new limit value; must be non-negative and no larger |
102 |
return pos; |
* than this buffer's capacity. |
103 |
} |
* |
104 |
|
* @exception IllegalArgumentException If the preconditions on newLimit |
105 |
|
* do not hold. |
106 |
|
*/ |
107 |
|
public final Buffer limit (int newLimit) |
108 |
|
{ |
109 |
|
if ((newLimit < 0) || (newLimit > cap)) |
110 |
|
throw new IllegalArgumentException (); |
111 |
|
|
112 |
|
if (newLimit <= mark) |
113 |
|
mark = -1; |
114 |
|
|
115 |
|
if (pos > newLimit) |
116 |
|
pos = newLimit - 1; |
117 |
|
|
118 |
|
limit = newLimit; |
119 |
|
return this; |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
|
* Sets this buffer's mark at its position. |
124 |
|
*/ |
125 |
|
public final Buffer mark () |
126 |
|
{ |
127 |
|
mark = pos; |
128 |
|
return this; |
129 |
|
} |
130 |
|
|
131 |
|
/** |
132 |
|
* Retrieves the current position of this buffer. |
133 |
|
*/ |
134 |
|
public final int position () |
135 |
|
{ |
136 |
|
return pos; |
137 |
|
} |
138 |
|
|
139 |
|
/** |
140 |
public final Buffer position(int newPosition) |
* Sets this buffer's position. If the mark is defined and larger than the |
141 |
{ |
* new position then it is discarded. |
142 |
/// If the mark is defined and larger than the new |
* |
143 |
|
* @param newPosition The new position value; must be non-negative and no |
144 |
if (newPosition <= mark) |
* larger than the current limit. |
145 |
mark = 0; |
* |
146 |
|
* @exception IllegalArgumentException If the preconditions on newPosition |
147 |
pos = newPosition; |
* do not hold |
148 |
return this; |
*/ |
149 |
} |
public final Buffer position (int newPosition) |
150 |
|
{ |
151 |
public final int remaining() |
if ((newPosition < 0) || (newPosition > limit)) |
152 |
{ |
throw new IllegalArgumentException (); |
153 |
return limit - pos; |
|
154 |
} |
if (newPosition <= mark) |
155 |
|
mark = -1; |
156 |
public final Buffer reset() |
|
157 |
{ |
pos = newPosition; |
158 |
pos = mark; |
return this; |
159 |
return this; |
} |
160 |
} |
|
161 |
|
/** |
162 |
public final Buffer rewind() |
* Returns the number of elements between the current position and the limit. |
163 |
{ |
*/ |
164 |
mark = pos = 0; |
public final int remaining() |
165 |
return this; |
{ |
166 |
} |
return limit - pos; |
167 |
|
} |
168 |
|
|
169 |
|
/** |
170 |
|
* Resets this buffer's position to the previously-marked position. |
171 |
|
* |
172 |
|
* @exception InvalidMarkException If the mark has not been set. |
173 |
|
*/ |
174 |
|
public final Buffer reset() |
175 |
|
{ |
176 |
|
if (mark == -1) |
177 |
|
throw new InvalidMarkException (); |
178 |
|
|
179 |
|
pos = mark; |
180 |
|
return this; |
181 |
|
} |
182 |
|
|
183 |
|
/** |
184 |
|
* Rewinds this buffer. The position is set to zero and the mark |
185 |
|
* is discarded. |
186 |
|
*/ |
187 |
|
public final Buffer rewind() |
188 |
|
{ |
189 |
|
pos = 0; |
190 |
|
mark = -1; |
191 |
|
return this; |
192 |
|
} |
193 |
} |
} |