Skip to content

Commit

Permalink
Split complex method, simplify close
Browse files Browse the repository at this point in the history
  • Loading branch information
mrotteveel committed Mar 4, 2024
1 parent 3ece8a4 commit cd38480
Showing 1 changed file with 43 additions and 62 deletions.
105 changes: 43 additions & 62 deletions src/main/org/firebirdsql/gds/ng/wire/WireConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public final C identify() throws SQLException {

sendConnectAttach(xdrOut);
int operation = handleCryptKeyCallbackBeforeAttachResponse();

if (operation == op_accept || operation == op_cond_accept || operation == op_accept_data) {
return handleConnectAttachAccept(xdrIn, operation);
} else {
Expand Down Expand Up @@ -465,51 +465,50 @@ private byte[] createUserIdentificationBlock() throws IOException, SQLException
}

void addServerKeys(byte[] serverKeys) throws SQLException {
final ClumpletReader newKeys = new ClumpletReader(ClumpletReader.Kind.UnTagged, serverKeys);
final var newKeys = new ClumpletReader(ClumpletReader.Kind.UnTagged, serverKeys);
for (newKeys.rewind(); !newKeys.isEof(); newKeys.moveNext()) {
int currentTag = newKeys.getClumpTag();
switch (currentTag) {
case TAG_KNOWN_PLUGINS:
// Nothing to do (yet)
break;
case TAG_PLUGIN_SPECIFIC:
addServerKey(newKeys);
}
}

private void addServerKey(ClumpletReader newKeys) throws SQLException {
int currentTag = newKeys.getClumpTag();
switch (currentTag) {
case TAG_KNOWN_PLUGINS -> {
// Nothing to do (yet)
}
case TAG_PLUGIN_SPECIFIC ->
// Nothing to do (yet)
log.log(DEBUG, "Possible implementation problem, found TAG_PLUGIN_SPECIFIC without TAG_KEY_TYPE");
break;
case TAG_KEY_TYPE: {
String keyType = newKeys.getString(StandardCharsets.ISO_8859_1);
case TAG_KEY_TYPE -> {
String keyType = newKeys.getString(StandardCharsets.ISO_8859_1);

newKeys.moveNext();
if (newKeys.isEof()) {
break;
}
currentTag = newKeys.getClumpTag();
if (currentTag != TAG_KEY_PLUGINS) {
throw new SQLException("Unexpected tag type: " + currentTag);
}
String keyPlugins = newKeys.getString(StandardCharsets.ISO_8859_1);

Map<String, byte[]> pluginSpecificData = null;
while (newKeys.directNext(TAG_PLUGIN_SPECIFIC)) {
byte[] data = newKeys.getBytes();
int sepIdx = ByteArrayHelper.indexOf(data, (byte) 0);
if (sepIdx > 0) {
String plugin = new String(data, 0, sepIdx, StandardCharsets.ISO_8859_1);
byte[] specificData = Arrays.copyOfRange(data, sepIdx + 1, data.length);
if (pluginSpecificData == null) {
pluginSpecificData = new HashMap<>();
}
pluginSpecificData.put(plugin, specificData);
newKeys.moveNext();
if (newKeys.isEof()) return;

currentTag = newKeys.getClumpTag();
if (currentTag != TAG_KEY_PLUGINS) {
throw new SQLException("Unexpected tag type: " + currentTag);
}
String keyPlugins = newKeys.getString(StandardCharsets.ISO_8859_1);

Map<String, byte[]> pluginSpecificData = null;
while (newKeys.directNext(TAG_PLUGIN_SPECIFIC)) {
byte[] data = newKeys.getBytes();
int sepIdx = ByteArrayHelper.indexOf(data, (byte) 0);
if (sepIdx > 0) {
String plugin = new String(data, 0, sepIdx, StandardCharsets.ISO_8859_1);
byte[] specificData = Arrays.copyOfRange(data, sepIdx + 1, data.length);
if (pluginSpecificData == null) {
pluginSpecificData = new HashMap<>();
}
pluginSpecificData.put(plugin, specificData);
}

knownServerKeys.add(new KnownServerKey(keyType, keyPlugins, pluginSpecificData));
break;
}
default:
log.log(DEBUG, "Ignored unexpected tag type: {0}", currentTag);
break;
}

knownServerKeys.add(new KnownServerKey(keyType, keyPlugins, pluginSpecificData));
}
default -> log.log(DEBUG, "Ignored unexpected tag type: {0}", currentTag);
}
}

Expand Down Expand Up @@ -564,30 +563,12 @@ public final int readNextOperation() throws IOException {
* @throws IOException
* if closing fails
*/
@SuppressWarnings("EmptyTryBlock")
public final void close() throws IOException {
IOException ioex = null;
try {
if (socket != null) {
try {
if (xdrOut != null) xdrOut.close();
} catch (IOException ex) {
ioex = ex;
}

try {
if (xdrIn != null) xdrIn.close();
} catch (IOException ex) {
if (ioex == null) ioex = ex;
}

try {
socket.close();
} catch (IOException ex) {
if (ioex == null) ioex = ex;
}

if (ioex != null) throw ioex;
}
try (var ignored1 = socket;
var ignored2 = xdrIn;
var ignored3 = xdrOut) {
// Ignored: Use try-with-resources to close
} finally {
xdrOut = null;
xdrIn = null;
Expand Down

0 comments on commit cd38480

Please sign in to comment.