1 | /* |
2 | Copyright (C) 2002-2004 MySQL AB |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of version 2 of the GNU General Public License as |
6 | published by the Free Software Foundation. |
7 | |
8 | There are special exceptions to the terms and conditions of the GPL |
9 | as it is applied to this software. View the full text of the |
10 | exception in file EXCEPTIONS-CONNECTOR-J in the directory of this |
11 | software distribution. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 | |
22 | |
23 | |
24 | */ |
25 | package com.mysql.jdbc; |
26 | |
27 | import java.io.UnsupportedEncodingException; |
28 | |
29 | import java.nio.ByteBuffer; |
30 | |
31 | import java.sql.SQLException; |
32 | |
33 | /** |
34 | * Buffer contains code to read and write packets from/to the MySQL server. |
35 | * |
36 | * @version $Id: Buffer.java 3726 2005-05-19 15:52:24Z mmatthews $ |
37 | * @author Mark Matthews |
38 | */ |
39 | abstract class Buffer { |
40 | static final int MAX_BYTES_TO_DUMP = 512; |
41 | |
42 | static final int NO_LENGTH_LIMIT = -1; |
43 | |
44 | static final long NULL_LENGTH = -1; |
45 | |
46 | public static Buffer allocateDirect(int size, boolean useNewIo) { |
47 | if (!useNewIo) { |
48 | return allocateNew(size, useNewIo); |
49 | } |
50 | |
51 | return new ChannelBuffer(size, true); |
52 | } |
53 | |
54 | public static Buffer allocateNew(byte[] buf, boolean useNewIo) { |
55 | if (!useNewIo) { |
56 | return new ByteArrayBuffer(buf); |
57 | } |
58 | |
59 | return new ChannelBuffer(buf); |
60 | } |
61 | |
62 | public static Buffer allocateNew(int size, boolean useNewIo) { |
63 | if (!useNewIo) { |
64 | return new ByteArrayBuffer(size); |
65 | } |
66 | |
67 | return new ChannelBuffer(size, true); |
68 | } |
69 | |
70 | protected boolean wasMultiPacket = false; |
71 | |
72 | abstract void clear(); |
73 | |
74 | final void dump() { |
75 | dump(getBufLength()); |
76 | } |
77 | |
78 | final String dump(int numBytes) { |
79 | return StringUtils.dumpAsHex(getBytes(0, |
80 | numBytes > getBufLength() ? getBufLength() : numBytes), |
81 | numBytes > getBufLength() ? getBufLength() : numBytes); |
82 | } |
83 | |
84 | final String dumpClampedBytes(int numBytes) { |
85 | int numBytesToDump = numBytes < MAX_BYTES_TO_DUMP ? numBytes |
86 | : MAX_BYTES_TO_DUMP; |
87 | |
88 | String dumped = StringUtils.dumpAsHex(getBytes(0, |
89 | numBytesToDump > getBufLength() ? getBufLength() |
90 | : numBytesToDump), |
91 | numBytesToDump > getBufLength() ? getBufLength() |
92 | : numBytesToDump); |
93 | |
94 | if (numBytesToDump < numBytes) { |
95 | return dumped + " ....(packet exceeds max. dump length)"; |
96 | } |
97 | |
98 | return dumped; |
99 | } |
100 | |
101 | final void dumpHeader() { |
102 | for (int i = 0; i < MysqlIO.HEADER_LENGTH; i++) { |
103 | String hexVal = Integer.toHexString(readByte(i) & 0xff); |
104 | |
105 | if (hexVal.length() == 1) { |
106 | hexVal = "0" + hexVal; //$NON-NLS-1$ |
107 | } |
108 | |
109 | System.out.print(hexVal + " "); //$NON-NLS-1$ |
110 | } |
111 | } |
112 | |
113 | final void dumpNBytes(int start, int nBytes) { |
114 | StringBuffer asciiBuf = new StringBuffer(); |
115 | |
116 | for (int i = start; (i < (start + nBytes)) && (i < getBufLength()); i++) { |
117 | String hexVal = Integer.toHexString(readByte(i) & 0xff); |
118 | |
119 | if (hexVal.length() == 1) { |
120 | hexVal = "0" + hexVal; //$NON-NLS-1$ |
121 | } |
122 | |
123 | System.out.print(hexVal + " "); //$NON-NLS-1$ |
124 | |
125 | if ((readByte(i) > 32) && (readByte(i) < 127)) { |
126 | asciiBuf.append((char) readByte(i)); |
127 | } else { |
128 | asciiBuf.append("."); //$NON-NLS-1$ |
129 | } |
130 | |
131 | asciiBuf.append(" "); //$NON-NLS-1$ |
132 | } |
133 | |
134 | System.out.println(" " + asciiBuf.toString()); //$NON-NLS-1$ |
135 | } |
136 | |
137 | abstract void ensureCapacity(int additionalData) throws SQLException; |
138 | |
139 | /** |
140 | * Skip over a length-encoded string |
141 | * |
142 | * @return The position past the end of the string |
143 | */ |
144 | public abstract int fastSkipLenString(); |
145 | |
146 | abstract int getBufLength(); |
147 | |
148 | /** |
149 | * Returns the array of bytes this Buffer is using to read from. |
150 | * |
151 | * @return byte array being read from |
152 | */ |
153 | public abstract byte[] getByteBuffer(); |
154 | |
155 | abstract byte[] getBytes(int len); |
156 | |
157 | abstract byte[] getBytes(int offset, int len); |
158 | |
159 | abstract int getCapacity(); |
160 | |
161 | /** |
162 | * Returns the Java NIO Buffer (if any) |
163 | */ |
164 | public abstract ByteBuffer getNioBuffer(); |
165 | |
166 | /** |
167 | * Returns the current position to write to/ read from |
168 | * |
169 | * @return the current position to write to/ read from |
170 | */ |
171 | public abstract int getPosition(); |
172 | |
173 | // 2000-06-05 Changed |
174 | abstract boolean isLastDataPacket(); |
175 | |
176 | abstract long newReadLength(); |
177 | |
178 | abstract byte readByte(); |
179 | |
180 | abstract byte readByte(int readAt); |
181 | |
182 | abstract long readFieldLength(); |
183 | |
184 | abstract int readInt(); |
185 | |
186 | abstract int readIntAsLong(); |
187 | |
188 | abstract byte[] readLenByteArray(int offset); |
189 | |
190 | abstract long readLength(); |
191 | |
192 | // 2000-06-05 Fixed |
193 | abstract long readLong(); |
194 | |
195 | // 2000-06-05 Changed |
196 | abstract int readLongInt(); |
197 | |
198 | // 2000-06-05 Fixed |
199 | abstract long readLongLong(); |
200 | |
201 | abstract int readnBytes(); |
202 | |
203 | // |
204 | // Read a null-terminated string |
205 | // |
206 | // To avoid alloc'ing a new byte array, we |
207 | // do this by hand, rather than calling getNullTerminatedBytes() |
208 | // |
209 | abstract String readString(); |
210 | |
211 | abstract String readString(String encoding) throws SQLException; |
212 | |
213 | abstract void setBufLength(int bufLength); |
214 | |
215 | /** |
216 | * Sets the array of bytes to use as a buffer to read from. |
217 | * |
218 | * @param byteBuffer |
219 | * the array of bytes to use as a buffer |
220 | */ |
221 | public abstract void setByteBuffer(byte[] byteBuffer); |
222 | |
223 | /** |
224 | * Set the current position to write to/ read from |
225 | * |
226 | * @param position |
227 | * the position (0-based index) |
228 | */ |
229 | public abstract void setPosition(int position); |
230 | |
231 | /** |
232 | * Sets whether this packet was part of a multipacket |
233 | * |
234 | * @param flag |
235 | * was this packet part of a multipacket? |
236 | */ |
237 | public void setWasMultiPacket(boolean flag) { |
238 | this.wasMultiPacket = flag; |
239 | } |
240 | |
241 | public String toString() { |
242 | return dumpClampedBytes(getPosition()); |
243 | } |
244 | |
245 | public String toSuperString() { |
246 | return super.toString(); |
247 | } |
248 | |
249 | /** |
250 | * Was this packet part of a multipacket? |
251 | * |
252 | * @return was this packet part of a multipacket? |
253 | */ |
254 | public boolean wasMultiPacket() { |
255 | return this.wasMultiPacket; |
256 | } |
257 | |
258 | abstract void writeByte(byte b) throws SQLException; |
259 | |
260 | // Write a byte array |
261 | abstract void writeBytesNoNull(byte[] bytes) throws SQLException; |
262 | |
263 | // Write a byte array with the given offset and length |
264 | abstract void writeBytesNoNull(byte[] bytes, int offset, int length) |
265 | throws SQLException; |
266 | |
267 | abstract void writeDouble(double d) throws SQLException; |
268 | |
269 | abstract void writeFieldLength(long length) throws SQLException; |
270 | |
271 | abstract void writeFloat(float f) throws SQLException; |
272 | |
273 | // 2000-06-05 Changed |
274 | abstract void writeInt(int i) throws SQLException; |
275 | |
276 | // Write a String using the specified character |
277 | // encoding |
278 | abstract void writeLenBytes(byte[] b) throws SQLException; |
279 | |
280 | // Write a String using the specified character |
281 | // encoding |
282 | abstract void writeLenString(String s, String encoding, |
283 | String serverEncoding, SingleByteCharsetConverter converter, |
284 | boolean parserKnowsUnicode) throws UnsupportedEncodingException, |
285 | SQLException; |
286 | |
287 | // 2000-06-05 Changed |
288 | abstract void writeLong(long i) throws SQLException; |
289 | |
290 | // 2000-06-05 Changed |
291 | abstract void writeLongInt(int i) throws SQLException; |
292 | |
293 | abstract void writeLongLong(long i) throws SQLException; |
294 | |
295 | // Write null-terminated string |
296 | abstract void writeString(String s) throws SQLException; |
297 | |
298 | // Write string, with no termination |
299 | abstract void writeStringNoNull(String s) throws SQLException; |
300 | |
301 | // Write a String using the specified character |
302 | // encoding |
303 | abstract void writeStringNoNull(String s, String encoding, |
304 | String serverEncoding, boolean parserKnowsUnicode) |
305 | throws UnsupportedEncodingException, SQLException; |
306 | } |