?? mysqlio.java
字號:
// necesarily the actual length of the byte array
// used as the buffer
if (reuse.getByteBuffer().length <= packetLength) {
reuse.setByteBuffer(new byte[packetLength + 1]);
}
// Set the new length
reuse.setBufLength(packetLength);
// Read the data from the server
readFully(this.mysqlInput, reuse.getByteBuffer(), 0, packetLength);
boolean isMultiPacket = false;
if (packetLength == maxThreeBytes) {
reuse.setPosition((int) maxThreeBytes);
int packetEndPoint = packetLength;
// it's multi-packet
isMultiPacket = true;
lengthRead = readFully(mysqlInput, this.packetHeaderBuf, 0, 4);
if (lengthRead < 4) {
forceClose();
throw new IOException("Unexpected end of input stream");
}
packetLength = ((int) (this.packetHeaderBuf[0] & 0xff))
+ (((int) (this.packetHeaderBuf[1] & 0xff)) << 8)
+ (((int) (this.packetHeaderBuf[2] & 0xff)) << 16);
Buffer multiPacket = new Buffer(packetLength);
boolean firstMultiPkt = true;
while (true) {
if (!firstMultiPkt) {
lengthRead = readFully(mysqlInput,
this.packetHeaderBuf, 0, 4);
if (lengthRead < 4) {
forceClose();
throw new IOException(
"Unexpected end of input stream");
}
packetLength = ((int) (this.packetHeaderBuf[0] & 0xff))
+ (((int) (this.packetHeaderBuf[1] & 0xff)) << 8)
+ (((int) (this.packetHeaderBuf[2] & 0xff)) << 16);
} else {
firstMultiPkt = false;
}
if (!this.useNewLargePackets && (packetLength == 1)) {
clearInputStream();
break;
} else if (packetLength < this.maxThreeBytes) {
byte newPacketSeq = this.packetHeaderBuf[3];
if (newPacketSeq != (multiPacketSeq + 1)) {
throw new IOException(
"Packets received out of order");
}
multiPacketSeq = newPacketSeq;
// Set the Buffer to it's original state
multiPacket.setPosition(0);
multiPacket.setSendLength(0);
// Set the new length
multiPacket.setBufLength(packetLength);
// Read the data from the server
byte[] byteBuf = multiPacket.getByteBuffer();
int lengthToWrite = packetLength;
int bytesRead = readFully(this.mysqlInput, byteBuf, 0,
packetLength);
if (bytesRead != lengthToWrite) {
throw new SQLException(
"Short read from server, expected "
+ lengthToWrite + " bytes, received only "
+ bytesRead + ".",
SQLError.SQL_STATE_COMMUNICATION_LINK_FAILURE);
}
reuse.writeBytesNoNull(byteBuf, 0, lengthToWrite);
packetEndPoint += lengthToWrite;
break; // end of multipacket sequence
}
byte newPacketSeq = this.packetHeaderBuf[3];
if (newPacketSeq != (multiPacketSeq + 1)) {
throw new IOException("Packets received out of order");
}
multiPacketSeq = newPacketSeq;
// Set the Buffer to it's original state
multiPacket.setPosition(0);
multiPacket.setSendLength(0);
// Set the new length
multiPacket.setBufLength(packetLength);
// Read the data from the server
byte[] byteBuf = multiPacket.getByteBuffer();
int lengthToWrite = packetLength;
int bytesRead = readFully(this.mysqlInput, byteBuf, 0,
packetLength);
if (bytesRead != lengthToWrite) {
throw new SQLException(
"Short read from server, expected " + lengthToWrite
+ " bytes, received only " + bytesRead + ".",
SQLError.SQL_STATE_COMMUNICATION_LINK_FAILURE);
}
reuse.writeBytesNoNull(byteBuf, 0, lengthToWrite);
packetEndPoint += lengthToWrite;
}
//reuse.writeByte((byte) 0);
reuse.setPosition(0);
reuse.setWasMultiPacket(true);
}
if (!isMultiPacket) {
reuse.getByteBuffer()[packetLength] = 0; // Null-termination
}
return reuse;
} catch (IOException ioEx) {
StringBuffer message = new StringBuffer(SQLError.get(
SQLError.SQL_STATE_COMMUNICATION_LINK_FAILURE));
message.append(": ");
message.append(ioEx.getClass().getName());
message.append(", underlying cause: ");
message.append(ioEx.getMessage());
if (!this.connection.useParanoidErrorMessages()) {
message.append(Util.stackTraceToString(ioEx));
}
throw new java.sql.SQLException(message.toString(),
SQLError.SQL_STATE_COMMUNICATION_LINK_FAILURE, 0);
}
}
/**
* Send a packet to the MySQL server
*
* @param packet DOCUMENT ME!
*
* @throws SQLException DOCUMENT ME!
*/
private final void send(Buffer packet) throws SQLException {
int l = packet.getPosition();
send(packet, l);
//
// Don't hold on to large packets
//
if (packet == this.sharedSendPacket) {
reclaimLargeSharedSendPacket();
}
}
private final void send(Buffer packet, int packetLen)
throws SQLException {
try {
if (packetLen > this.maxAllowedPacket) {
throw new PacketTooBigException(packetLen, this.maxAllowedPacket);
}
if ((serverMajorVersion >= 4) && (packetLen >= maxThreeBytes)) {
sendSplitPackets(packet);
} else {
this.packetSequence++;
Buffer packetToSend = packet;
packetToSend.setPosition(0);
if (this.useCompression) {
packetToSend = compressPacket(packet, 0, packetLen,
HEADER_LENGTH);
packetLen = packetToSend.getPosition();
} else {
packetToSend.writeLongInt(packetLen - HEADER_LENGTH);
packetToSend.writeByte(this.packetSequence);
}
this.mysqlOutput.write(packetToSend.getByteBuffer(), 0,
packetLen);
this.mysqlOutput.flush();
}
//
// Don't hold on to large packets
//
if (packet == this.sharedSendPacket) {
reclaimLargeSharedSendPacket();
}
} catch (IOException ioEx) {
StringBuffer message = new StringBuffer(SQLError.get(
SQLError.SQL_STATE_COMMUNICATION_LINK_FAILURE));
message.append(": ");
message.append(ioEx.getClass().getName());
message.append(", underlying cause: ");
message.append(ioEx.getMessage());
if (!this.connection.useParanoidErrorMessages()) {
message.append(Util.stackTraceToString(ioEx));
}
throw new java.sql.SQLException(message.toString(),
SQLError.SQL_STATE_COMMUNICATION_LINK_FAILURE, 0);
}
}
/**
* Reads and sends a file to the server for LOAD DATA LOCAL INFILE
*
* @param fileName the file name to send.
*
* @return DOCUMENT ME!
*
* @throws SQLException DOCUMENT ME!
*/
private final ResultSet sendFileToServer(String fileName)
throws SQLException {
Buffer filePacket = (loadFileBufRef == null) ? null
: (Buffer) (loadFileBufRef
.get());
int packetLength = Math.min(this.connection.getMaxAllowedPacket()
- (HEADER_LENGTH * 3),
alignPacketSize(this.connection.getMaxAllowedPacket() - 16, 4096)
- (HEADER_LENGTH * 3));
//
// This packet may be _way_ too large to actually allocate,
// unforunately, LOAD DATA LOCAL INFILE requires this setup...
//
try {
if (filePacket == null) {
filePacket = new Buffer((int) (packetLength + HEADER_LENGTH));
loadFileBufRef = new SoftReference(filePacket);
}
} catch (OutOfMemoryError oom) {
// Attempt to do this, but it might not work...
// The server is expecting at least one packet, so we
// send an empty 'EOF' packet...
this.reusablePacket.clear();
send(this.reusablePacket);
throw new SQLException("Unable to allocate packet of size '"
+ (packetLength + HEADER_LENGTH)
+ "' for LOAD DATA LOCAL INFILE. Either increase heap space available to your JVM, or adjust the MySQL server variable 'max_allowed_packet'",
SQLError.SQL_STATE_MEMORY_ALLOCATION_FAILURE);
}
filePacket.clear();
send(filePacket, 0);
byte[] fileBuf = new byte[packetLength];
BufferedInputStream fileIn = null;
try {
fileIn = new BufferedInputStream(new FileInputStream(fileName));
int bytesRead = 0;
while ((bytesRead = fileIn.read(fileBuf)) != -1) {
filePacket.clear();
filePacket.writeBytesNoNull(fileBuf, 0, bytesRead);
send(filePacket);
}
} catch (IOException ioEx) {
StringBuffer messageBuf = new StringBuffer("Unable to open file ");
if (!this.connection.useParanoidErrorMessages()) {
messageBuf.append("'");
if (fileName != null) {
messageBuf.append(fileName);
}
messageBuf.append("'");
}
messageBuf.append("for 'LOAD DATA LOCAL INFILE' command.");
if (!this.connection.useParanoidErrorMessages()) {
messageBuf.append("Due to underlying IOException: ");
messageBuf.append(Util.stackTraceToString(ioEx));
}
throw new SQLException(messageBuf.toString(),
SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
} finally {
if (fileIn != null) {
try {
fileIn.close();
} catch (Exception ex) {
throw new SQLException("Unable to close local file during LOAD DATA LOCAL INFILE command",
SQLError.SQL_STATE_GENERAL_ERROR);
}
fileIn = null;
} else {
// file open failed, but server needs one packet
filePacket.clear();
send(filePacket);
}
}
// send empty packet to mark EOF
filePacket.clear();
send(filePacket);
Buffer resultPacket = checkErrorPacket();
return buildResultSetWithUpdates(resultPacket);
}
/**
* Checks for errors in the reply packet, and if none, returns the reply
* packet, ready for reading
*
* @return DOCUMENT ME!
*
* @throws SQLException DOCUMENT ME!
*/
private Buffer checkErrorPacket() throws SQLException {
return checkErrorPacket(-1);
}
/**
* Checks for errors in the reply packet, and if none, returns the reply
* packet, ready for reading
*
* @param command the command being issued (if used)
*
* @return DOCUMENT ME!
*
* @throws SQLException if an error packet was received
* @throws java.sql.SQLException DOCUMENT ME!
*/
private Buffer checkErrorPacket(int command) throws SQLException {
int statusCode = 0;
Buffer resultPacket = null;
try {
// Check return value, if we get a java.io.EOFException,
/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -