flush gzipped streams in finally and try-catch

pull/53/head
Zlatin Balevsky 2020-10-21 14:32:59 +01:00
parent 688beae1d6
commit 917c231d51
No known key found for this signature in database
GPG Key ID: A72832072D525E41
2 changed files with 27 additions and 20 deletions

View File

@ -349,6 +349,7 @@ class ConnectionAcceptor {
} }
private void processBROWSE(Endpoint e) { private void processBROWSE(Endpoint e) {
DataOutputStream dos = null
try { try {
byte [] rowse = new byte[7] byte [] rowse = new byte[7]
DataInputStream dis = new DataInputStream(e.getInputStream()) DataInputStream dis = new DataInputStream(e.getInputStream())
@ -388,7 +389,7 @@ class ConnectionAcceptor {
os.write("\r\n".getBytes(StandardCharsets.US_ASCII)) os.write("\r\n".getBytes(StandardCharsets.US_ASCII))
DataOutputStream dos = new DataOutputStream(new GZIPOutputStream(os)) dos = new DataOutputStream(new GZIPOutputStream(os))
JsonOutput jsonOutput = new JsonOutput() JsonOutput jsonOutput = new JsonOutput()
sharedFiles.each { sharedFiles.each {
it.hit(browser, System.currentTimeMillis(), "Browse Host"); it.hit(browser, System.currentTimeMillis(), "Browse Host");
@ -398,16 +399,17 @@ class ConnectionAcceptor {
dos.writeShort((short)json.length()) dos.writeShort((short)json.length())
dos.write(json.getBytes(StandardCharsets.US_ASCII)) dos.write(json.getBytes(StandardCharsets.US_ASCII))
} }
dos.flush()
try {
dos.close()
} catch (Exception ignored) {}
} finally { } finally {
try {
dos?.flush()
dos?.close()
} catch (Exception ignored) {}
e.close() e.close()
} }
} }
private void processTRUST(Endpoint e) { private void processTRUST(Endpoint e) {
DataOutputStream dos = null
try { try {
byte[] RUST = new byte[6] byte[] RUST = new byte[6]
DataInputStream dis = new DataInputStream(e.getInputStream()) DataInputStream dis = new DataInputStream(e.getInputStream())
@ -431,7 +433,7 @@ class ConnectionAcceptor {
List<TrustService.TrustEntry> good = new ArrayList<>(trustService.good.values()) List<TrustService.TrustEntry> good = new ArrayList<>(trustService.good.values())
List<TrustService.TrustEntry> bad = new ArrayList<>(trustService.bad.values()) List<TrustService.TrustEntry> bad = new ArrayList<>(trustService.bad.values())
DataOutputStream dos = new DataOutputStream(os) dos = new DataOutputStream(os)
if (!json) { if (!json) {
os.write("\r\n".getBytes(StandardCharsets.US_ASCII)) os.write("\r\n".getBytes(StandardCharsets.US_ASCII))
@ -474,13 +476,16 @@ class ConnectionAcceptor {
} }
} }
dos.flush()
} finally { } finally {
try {
dos?.flush()
} catch (IOException ignore) {}
e.close() e.close()
} }
} }
private void processCERTIFICATES(Endpoint e) { private void processCERTIFICATES(Endpoint e) {
DataOutputStream dos = null
try { try {
byte [] ERTIFICATES = new byte[12] byte [] ERTIFICATES = new byte[12]
DataInputStream dis = new DataInputStream(e.getInputStream()) DataInputStream dis = new DataInputStream(e.getInputStream())
@ -516,7 +521,7 @@ class ConnectionAcceptor {
os.write("Count: ${certs.size()}\r\n".getBytes(StandardCharsets.US_ASCII)) os.write("Count: ${certs.size()}\r\n".getBytes(StandardCharsets.US_ASCII))
os.write("\r\n".getBytes(StandardCharsets.US_ASCII)) os.write("\r\n".getBytes(StandardCharsets.US_ASCII))
DataOutputStream dos = new DataOutputStream(os) dos = new DataOutputStream(os)
certs.each { certs.each {
ByteArrayOutputStream baos = new ByteArrayOutputStream() ByteArrayOutputStream baos = new ByteArrayOutputStream()
it.write(baos) it.write(baos)
@ -524,10 +529,10 @@ class ConnectionAcceptor {
dos.writeShort(payload.length) dos.writeShort(payload.length)
dos.write(payload) dos.write(payload)
} }
try {
dos.close()
} catch (Exception ignored) {}
} finally { } finally {
try {
dos?.close()
} catch (Exception ignored) {}
e.close() e.close()
} }
} }
@ -542,6 +547,7 @@ class ConnectionAcceptor {
} }
private void processFEED(Endpoint e) { private void processFEED(Endpoint e) {
DataOutputStream dos = null
try { try {
byte[] EED = new byte[5]; byte[] EED = new byte[5];
DataInputStream dis = new DataInputStream(e.getInputStream()) DataInputStream dis = new DataInputStream(e.getInputStream())
@ -576,7 +582,7 @@ class ConnectionAcceptor {
os.write("Count: ${published.size()}\r\n".getBytes(StandardCharsets.US_ASCII)); os.write("Count: ${published.size()}\r\n".getBytes(StandardCharsets.US_ASCII));
os.write("\r\n".getBytes(StandardCharsets.US_ASCII)) os.write("\r\n".getBytes(StandardCharsets.US_ASCII))
DataOutputStream dos = new DataOutputStream(new GZIPOutputStream(os)) dos = new DataOutputStream(new GZIPOutputStream(os))
JsonOutput jsonOutput = new JsonOutput() JsonOutput jsonOutput = new JsonOutput()
final long now = System.currentTimeMillis(); final long now = System.currentTimeMillis();
published.each { published.each {
@ -587,11 +593,11 @@ class ConnectionAcceptor {
dos.writeShort((short)json.length()) dos.writeShort((short)json.length())
dos.write(json.getBytes(StandardCharsets.US_ASCII)) dos.write(json.getBytes(StandardCharsets.US_ASCII))
} }
dos.flush()
try {
dos.close()
} catch(Exception ignore) {}
} finally { } finally {
try {
dos?.flush()
dos?.close()
} catch(Exception ignore) {}
e.close() e.close()
} }
} }

View File

@ -131,6 +131,7 @@ class ResultsSender {
endpoint?.close() endpoint?.close()
} }
} else { } else {
DataOutputStream dos = null
try { try {
endpoint = connector.connect(target) endpoint = connector.connect(target)
OutputStream os = endpoint.getOutputStream() OutputStream os = endpoint.getOutputStream()
@ -142,7 +143,7 @@ class ResultsSender {
boolean feed = settings.fileFeed && settings.advertiseFeed boolean feed = settings.fileFeed && settings.advertiseFeed
os.write("Feed: $feed\r\n".getBytes(StandardCharsets.US_ASCII)) os.write("Feed: $feed\r\n".getBytes(StandardCharsets.US_ASCII))
os.write("\r\n".getBytes(StandardCharsets.US_ASCII)) os.write("\r\n".getBytes(StandardCharsets.US_ASCII))
DataOutputStream dos = new DataOutputStream(new GZIPOutputStream(os)) dos = new DataOutputStream(new GZIPOutputStream(os))
results.each { results.each {
int certificates = certificateManager.getByInfoHash(new InfoHash(it.getRoot())).size() int certificates = certificateManager.getByInfoHash(new InfoHash(it.getRoot())).size()
def obj = sharedFileToObj(it, settings.browseFiles, certificates) def obj = sharedFileToObj(it, settings.browseFiles, certificates)
@ -150,10 +151,10 @@ class ResultsSender {
dos.writeShort((short)json.length()) dos.writeShort((short)json.length())
dos.write(json.getBytes(StandardCharsets.US_ASCII)) dos.write(json.getBytes(StandardCharsets.US_ASCII))
} }
try {
dos.close()
} catch (Exception ignore) {}
} finally { } finally {
try {
dos?.close()
} catch (Exception ignore) {}
endpoint?.close() endpoint?.close()
} }
} }