Skip to content

Commit

Permalink
[misc] code simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Mar 25, 2021
1 parent f737786 commit 62f626d
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 155 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/mariadb/jdbc/client/ClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public ClientImpl(
if (conf.socketTimeout() > 0) setSocketTimeout(conf.socketTimeout());

// read server handshake
ReadableByteBuf buf = reader.readReadablePacket(true);
ReadableByteBuf buf = reader.readPacket(true);
if (buf.getByte() == -1) {
ErrorPacket errorPacket = new ErrorPacket(buf, null);
throw this.exceptionFactory.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public static void authenticationHandler(

writer.permitTrace(true);
Configuration conf = context.getConf();
ReadableByteBuf buf = reader.readReadablePacket(false);
ReadableByteBuf buf = reader.readPacket(false);

authentication_loop:
while (true) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/mariadb/jdbc/client/result/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public Result(ColumnDefinitionPacket[] metadataList, byte[][] data, Context cont

@SuppressWarnings("fallthrough")
protected boolean readNext() throws SQLException, IOException {
byte[] buf = reader.readPacket(false, traceEnable);
byte[] buf = reader.readPacket(false, traceEnable).buf();
switch (buf[0]) {
case (byte) 0xFF:
loaded = true;
Expand Down Expand Up @@ -159,7 +159,7 @@ protected boolean readNext() throws SQLException, IOException {
@SuppressWarnings("fallthrough")
protected void skipRemaining() throws SQLException, IOException {
while (true) {
ReadableByteBuf buf = reader.readReadablePacket(true, traceEnable);
ReadableByteBuf buf = reader.readPacket(true, traceEnable);
switch (buf.getUnsignedByte()) {
case 0xFF:
loaded = true;
Expand Down
125 changes: 3 additions & 122 deletions src/main/java/org/mariadb/jdbc/client/socket/PacketReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public PacketReader(InputStream in, Configuration conf, MutableInt sequence) {
* @return array packet.
* @throws IOException if socket exception occur.
*/
public ReadableByteBuf readReadablePacket(boolean reUsable) throws IOException {
return readReadablePacket(reUsable, logger.isTraceEnabled());
public ReadableByteBuf readPacket(boolean reUsable) throws IOException {
return readPacket(reUsable, logger.isTraceEnabled());
}

/**
Expand All @@ -79,8 +79,7 @@ public ReadableByteBuf readReadablePacket(boolean reUsable) throws IOException {
* @return array packet.
* @throws IOException if socket exception occur.
*/
public ReadableByteBuf readReadablePacket(boolean reUsable, boolean traceEnable)
throws IOException {
public ReadableByteBuf readPacket(boolean reUsable, boolean traceEnable) throws IOException {
// ***************************************************
// Read 4 byte header
// ***************************************************
Expand Down Expand Up @@ -189,129 +188,11 @@ public ReadableByteBuf readReadablePacket(boolean reUsable, boolean traceEnable)

lastPacketLength += packetLength;
} while (packetLength == MAX_PACKET_SIZE);

return new ReadableByteBuf(sequence, rawBytes, rawBytes.length);
}

return new ReadableByteBuf(sequence, rawBytes, lastPacketLength);
}

public byte[] readPacket(boolean reUsable, boolean traceEnable) throws IOException {
// ***************************************************
// Read 4 byte header
// ***************************************************
int remaining = 4;
int off = 0;
do {
int count = inputStream.read(header, off, remaining);
if (count < 0) {
throw new EOFException(
"unexpected end of stream, read "
+ off
+ " bytes from 4 (socket was closed by server)");
}
remaining -= count;
off += count;
} while (remaining > 0);

int lastPacketLength =
(header[0] & 0xff) + ((header[1] & 0xff) << 8) + ((header[2] & 0xff) << 16);
sequence.set(header[3]);

// prepare array
byte[] rawBytes;
if (reUsable && lastPacketLength < REUSABLE_BUFFER_LENGTH) {
rawBytes = reusableArray;
} else {
rawBytes = new byte[lastPacketLength];
}

// ***************************************************
// Read content
// ***************************************************
remaining = lastPacketLength;
off = 0;
do {
int count = inputStream.read(rawBytes, off, remaining);
if (count < 0) {
throw new EOFException(
"unexpected end of stream, read "
+ (lastPacketLength - remaining)
+ " bytes from "
+ lastPacketLength
+ " (socket was closed by server)");
}
remaining -= count;
off += count;
} while (remaining > 0);

if (traceEnable) {
logger.trace(
"read: {}\n{}",
serverThreadLog,
LoggerHelper.hex(header, rawBytes, 0, lastPacketLength, maxQuerySizeToLog));
}

// ***************************************************
// In case content length is big, content will be separate in many 16Mb packets
// ***************************************************
if (lastPacketLength == MAX_PACKET_SIZE) {
int packetLength;
do {
remaining = 4;
off = 0;
do {
int count = inputStream.read(header, off, remaining);
if (count < 0) {
throw new EOFException("unexpected end of stream, read " + off + " bytes from 4");
}
remaining -= count;
off += count;
} while (remaining > 0);

packetLength = (header[0] & 0xff) + ((header[1] & 0xff) << 8) + ((header[2] & 0xff) << 16);
sequence.set(header[3]);

int currentbufLength = rawBytes.length;
byte[] newRawBytes = new byte[currentbufLength + packetLength];
System.arraycopy(rawBytes, 0, newRawBytes, 0, currentbufLength);
rawBytes = newRawBytes;

// ***************************************************
// Read content
// ***************************************************
remaining = packetLength;
off = currentbufLength;
do {
int count = inputStream.read(rawBytes, off, remaining);
if (count < 0) {
throw new EOFException(
"unexpected end of stream, read "
+ (packetLength - remaining)
+ " bytes from "
+ packetLength);
}
remaining -= count;
off += count;
} while (remaining > 0);

if (traceEnable) {
logger.trace(
"read: {}\n{}",
serverThreadLog,
LoggerHelper.hex(
header, rawBytes, currentbufLength, packetLength, maxQuerySizeToLog));
}

lastPacketLength += packetLength;
} while (packetLength == MAX_PACKET_SIZE);

return rawBytes;
}

return rawBytes;
}

public MutableInt getSequence() {
return sequence;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,7 @@ public void writeByte(int value) throws IOException {
public void writeShort(short value) throws IOException {
if (2 > buf.length - pos) {
// not enough space remaining
byte[] arr = new byte[2];
arr[0] = (byte) value;
arr[1] = (byte) (value >> 8);
writeBytes(arr, 0, 2);
writeBytes(new byte[] {(byte) value, (byte) (value >> 8)}, 0, 2);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ default Completion readPacket(
boolean traceEnable)
throws IOException, SQLException {

ReadableByteBuf buf = reader.readReadablePacket(true, traceEnable);
ReadableByteBuf buf = reader.readPacket(true, traceEnable);

switch (buf.getUnsignedByte()) {

Expand Down Expand Up @@ -157,14 +157,14 @@ default Completion readPacket(
for (int i = 0; i < fieldCount; i++) {
ci[i] =
new ColumnDefinitionPacket(
reader.readReadablePacket(false, traceEnable),
reader.readPacket(false, traceEnable),
(context.getServerCapabilities() & Capabilities.MARIADB_CLIENT_EXTENDED_TYPE_INFO)
> 0);
}

if (!context.isEofDeprecated()) {
// skip intermediate EOF
reader.readReadablePacket(true, traceEnable);
reader.readPacket(true, traceEnable);
}

// read resultSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public Completion readPacket(
boolean traceEnable)
throws IOException, SQLException {

ReadableByteBuf buf = reader.readReadablePacket(true, traceEnable);
ReadableByteBuf buf = reader.readPacket(true, traceEnable);
switch (buf.getUnsignedByte()) {
// *********************************************************************************************************
// * ERROR response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,24 @@ public PrepareResultPacket(ReadableByteBuf buffer, PacketReader reader, Context
for (int i = 0; i < numParams; i++) {
parameters[i] =
new ColumnDefinitionPacket(
reader.readReadablePacket(false, trace),
reader.readPacket(false, trace),
(context.getServerCapabilities() & Capabilities.MARIADB_CLIENT_EXTENDED_TYPE_INFO)
> 0);
}
if (!context.isEofDeprecated()) {
reader.readReadablePacket(true, trace);
reader.readPacket(true, trace);
}
}
if (numColumns > 0) {
for (int i = 0; i < numColumns; i++) {
columns[i] =
new ColumnDefinitionPacket(
reader.readReadablePacket(false, trace),
reader.readPacket(false, trace),
(context.getServerCapabilities() & Capabilities.MARIADB_CLIENT_EXTENDED_TYPE_INFO)
> 0);
}
if (!context.isEofDeprecated()) {
reader.readReadablePacket(true, trace);
reader.readPacket(true, trace);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public ReadableByteBuf process(PacketWriter out, PacketReader in, Context contex
out.flush();
}

ReadableByteBuf buf = in.readReadablePacket(true);
ReadableByteBuf buf = in.readPacket(true);

switch (buf.getByte()) {

Expand All @@ -146,7 +146,7 @@ public ReadableByteBuf process(PacketWriter out, PacketReader in, Context contex
buf.readBytes(authResult);
switch (authResult[0]) {
case 3:
return in.readReadablePacket(true);
return in.readPacket(true);
case 4:
if (conf.sslMode() != SslMode.DISABLE) {
// send clear password
Expand Down Expand Up @@ -187,7 +187,7 @@ public ReadableByteBuf process(PacketWriter out, PacketReader in, Context contex
}
}

return in.readReadablePacket(true);
return in.readPacket(true);

default:
throw new SQLException(
Expand Down Expand Up @@ -230,7 +230,7 @@ public static PublicKey readPublicKeyFromFile(String serverRsaPublicKeyFile) thr
*/
public static PublicKey readPublicKeyFromSocket(PacketReader reader, Context context)
throws SQLException, IOException {
ReadableByteBuf buf = reader.readReadablePacket(true);
ReadableByteBuf buf = reader.readPacket(true);

switch (buf.getByte(0)) {
case (byte) 0xFF:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ public ReadableByteBuf process(PacketWriter out, PacketReader in, Context contex
out.flush();
}

return in.readReadablePacket(true);
return in.readPacket(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,6 @@ public ReadableByteBuf process(PacketWriter out, PacketReader in, Context contex
out.flush();
}

return in.readReadablePacket(true);
return in.readPacket(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,6 @@ public ReadableByteBuf process(PacketWriter out, PacketReader in, Context contex
}
}

return in.readReadablePacket(true);
return in.readPacket(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ public ReadableByteBuf process(PacketWriter out, PacketReader in, Context contex

gssapiAuth.authenticate(out, in, servicePrincipalName, mechanisms);

return in.readReadablePacket(true);
return in.readPacket(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public ReadableByteBuf process(PacketWriter out, PacketReader in, Context contex
out.writeByte(0);
out.flush();

ReadableByteBuf buf = in.readReadablePacket(true);
ReadableByteBuf buf = in.readPacket(true);

int type = buf.getUnsignedByte();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void authenticate(
out.flush();
}
if (!context.isEstablished()) {
ReadableByteBuf buf = in.readReadablePacket(true);
ReadableByteBuf buf = in.readPacket(true);
inToken = new byte[buf.readableBytes()];
buf.readBytes(inToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void authenticate(

// Step 2: read server response token
if (clientContext.isContinue()) {
ReadableByteBuf buf = in.readReadablePacket(true);
ReadableByteBuf buf = in.readPacket(true);
byte[] tokenForTheClientOnTheServer = new byte[buf.readableBytes()];
buf.readBytes(tokenForTheClientOnTheServer);
Sspi.SecBufferDesc continueToken =
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/org/mariadb/jdbc/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ public Connection createProxyCon(HaMode mode, String opts) throws SQLException {

public static boolean haveSsl() throws SQLException {
Statement stmt = sharedConn.createStatement();
ResultSet rs = stmt.executeQuery("select @@have_ssl");
ResultSet rs = stmt.executeQuery("show variables like '%ssl%'");
while (rs.next()) {
System.out.println(rs.getString(1) + ":" + rs.getString(2));
}

rs = stmt.executeQuery("select @@have_ssl");
assertTrue(rs.next());
return "YES".equals(rs.getString(1));
}
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/org/mariadb/jdbc/integration/LoggingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ void basicLogging() throws Exception {
+ "| 73 69 6F 6E 5F 74 72 61 63 6B 5F 73 63 68 65 6D | sion_track_schem |\n"
+ "| 61 3D 31 | a=1 |\n"
+ "+--------------------------------------------------+------------------+\n";

Assertions.assertTrue(
contents.contains(defaultRequest)
|| contents.contains(defaultRequest.replace("\r\n", "\n")));

if (!"maxscale".equals(System.getenv("srv")) && !"skysql-ha".equals(System.getenv("srv"))) {
Assertions.assertTrue(
contents.contains(defaultRequest)
|| contents.contains(defaultRequest.replace("\r\n", "\n")));
}
String selectOne =
"+--------------------------------------------------+\n"
+ "| 0 1 2 3 4 5 6 7 8 9 a b c d e f |\n"
Expand Down

0 comments on commit 62f626d

Please sign in to comment.