mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Use try-with-ressource statements instead of manually closing stream
This commit is contained in:
parent
c3bfcff5a8
commit
447a188ff9
1 changed files with 35 additions and 57 deletions
|
@ -1223,26 +1223,26 @@ class Manager implements Signal {
|
||||||
|
|
||||||
private void storeEnvelope(SignalServiceEnvelope envelope, File file) throws IOException {
|
private void storeEnvelope(SignalServiceEnvelope envelope, File file) throws IOException {
|
||||||
try (FileOutputStream f = new FileOutputStream(file)) {
|
try (FileOutputStream f = new FileOutputStream(file)) {
|
||||||
DataOutputStream out = new DataOutputStream(f);
|
try (DataOutputStream out = new DataOutputStream(f)) {
|
||||||
out.writeInt(1); // version
|
out.writeInt(1); // version
|
||||||
out.writeInt(envelope.getType());
|
out.writeInt(envelope.getType());
|
||||||
out.writeUTF(envelope.getSource());
|
out.writeUTF(envelope.getSource());
|
||||||
out.writeInt(envelope.getSourceDevice());
|
out.writeInt(envelope.getSourceDevice());
|
||||||
out.writeUTF(envelope.getRelay());
|
out.writeUTF(envelope.getRelay());
|
||||||
out.writeLong(envelope.getTimestamp());
|
out.writeLong(envelope.getTimestamp());
|
||||||
if (envelope.hasContent()) {
|
if (envelope.hasContent()) {
|
||||||
out.writeInt(envelope.getContent().length);
|
out.writeInt(envelope.getContent().length);
|
||||||
out.write(envelope.getContent());
|
out.write(envelope.getContent());
|
||||||
} else {
|
} else {
|
||||||
out.writeInt(0);
|
out.writeInt(0);
|
||||||
|
}
|
||||||
|
if (envelope.hasLegacyMessage()) {
|
||||||
|
out.writeInt(envelope.getLegacyMessage().length);
|
||||||
|
out.write(envelope.getLegacyMessage());
|
||||||
|
} else {
|
||||||
|
out.writeInt(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (envelope.hasLegacyMessage()) {
|
|
||||||
out.writeInt(envelope.getLegacyMessage().length);
|
|
||||||
out.write(envelope.getLegacyMessage());
|
|
||||||
} else {
|
|
||||||
out.writeInt(0);
|
|
||||||
}
|
|
||||||
out.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1288,9 +1288,7 @@ class Manager implements Signal {
|
||||||
private File retrieveAttachment(SignalServiceAttachmentStream stream, File outputFile) throws IOException, InvalidMessageException {
|
private File retrieveAttachment(SignalServiceAttachmentStream stream, File outputFile) throws IOException, InvalidMessageException {
|
||||||
InputStream input = stream.getInputStream();
|
InputStream input = stream.getInputStream();
|
||||||
|
|
||||||
OutputStream output = null;
|
try (OutputStream output = new FileOutputStream(outputFile)) {
|
||||||
try {
|
|
||||||
output = new FileOutputStream(outputFile);
|
|
||||||
byte[] buffer = new byte[4096];
|
byte[] buffer = new byte[4096];
|
||||||
int read;
|
int read;
|
||||||
|
|
||||||
|
@ -1300,10 +1298,6 @@ class Manager implements Signal {
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
|
||||||
if (output != null) {
|
|
||||||
output.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return outputFile;
|
return outputFile;
|
||||||
}
|
}
|
||||||
|
@ -1311,43 +1305,31 @@ class Manager implements Signal {
|
||||||
private File retrieveAttachment(SignalServiceAttachmentPointer pointer, File outputFile, boolean storePreview) throws IOException, InvalidMessageException {
|
private File retrieveAttachment(SignalServiceAttachmentPointer pointer, File outputFile, boolean storePreview) throws IOException, InvalidMessageException {
|
||||||
if (storePreview && pointer.getPreview().isPresent()) {
|
if (storePreview && pointer.getPreview().isPresent()) {
|
||||||
File previewFile = new File(outputFile + ".preview");
|
File previewFile = new File(outputFile + ".preview");
|
||||||
OutputStream output = null;
|
try (OutputStream output = new FileOutputStream(previewFile)) {
|
||||||
try {
|
|
||||||
output = new FileOutputStream(previewFile);
|
|
||||||
byte[] preview = pointer.getPreview().get();
|
byte[] preview = pointer.getPreview().get();
|
||||||
output.write(preview, 0, preview.length);
|
output.write(preview, 0, preview.length);
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
|
||||||
if (output != null) {
|
|
||||||
output.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, deviceId, signalingKey, USER_AGENT);
|
final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, deviceId, signalingKey, USER_AGENT);
|
||||||
|
|
||||||
File tmpFile = Util.createTempFile();
|
File tmpFile = Util.createTempFile();
|
||||||
InputStream input = messageReceiver.retrieveAttachment(pointer, tmpFile);
|
try (InputStream input = messageReceiver.retrieveAttachment(pointer, tmpFile)) {
|
||||||
|
try (OutputStream output = new FileOutputStream(outputFile)) {
|
||||||
|
byte[] buffer = new byte[4096];
|
||||||
|
int read;
|
||||||
|
|
||||||
OutputStream output = null;
|
while ((read = input.read(buffer)) != -1) {
|
||||||
try {
|
output.write(buffer, 0, read);
|
||||||
output = new FileOutputStream(outputFile);
|
}
|
||||||
byte[] buffer = new byte[4096];
|
} catch (FileNotFoundException e) {
|
||||||
int read;
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
while ((read = input.read(buffer)) != -1) {
|
|
||||||
output.write(buffer, 0, read);
|
|
||||||
}
|
}
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
} finally {
|
} finally {
|
||||||
if (output != null) {
|
|
||||||
output.close();
|
|
||||||
}
|
|
||||||
input.close();
|
|
||||||
try {
|
try {
|
||||||
Files.delete(tmpFile.toPath());
|
Files.delete(tmpFile.toPath());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -1381,15 +1363,13 @@ class Manager implements Signal {
|
||||||
File groupsFile = Util.createTempFile();
|
File groupsFile = Util.createTempFile();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(new FileOutputStream(groupsFile));
|
try (OutputStream fos = new FileOutputStream(groupsFile)) {
|
||||||
try {
|
DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(fos);
|
||||||
for (GroupInfo record : groupStore.getGroups()) {
|
for (GroupInfo record : groupStore.getGroups()) {
|
||||||
out.write(new DeviceGroup(record.groupId, Optional.fromNullable(record.name),
|
out.write(new DeviceGroup(record.groupId, Optional.fromNullable(record.name),
|
||||||
new ArrayList<>(record.members), createGroupAvatarAttachment(record.groupId),
|
new ArrayList<>(record.members), createGroupAvatarAttachment(record.groupId),
|
||||||
record.active));
|
record.active));
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
out.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (groupsFile.exists() && groupsFile.length() > 0) {
|
if (groupsFile.exists() && groupsFile.length() > 0) {
|
||||||
|
@ -1416,14 +1396,12 @@ class Manager implements Signal {
|
||||||
File contactsFile = Util.createTempFile();
|
File contactsFile = Util.createTempFile();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
DeviceContactsOutputStream out = new DeviceContactsOutputStream(new FileOutputStream(contactsFile));
|
try (OutputStream fos = new FileOutputStream(contactsFile)) {
|
||||||
try {
|
DeviceContactsOutputStream out = new DeviceContactsOutputStream(fos);
|
||||||
for (ContactInfo record : contactStore.getContacts()) {
|
for (ContactInfo record : contactStore.getContacts()) {
|
||||||
out.write(new DeviceContact(record.number, Optional.fromNullable(record.name),
|
out.write(new DeviceContact(record.number, Optional.fromNullable(record.name),
|
||||||
createContactAvatarAttachment(record.number), Optional.fromNullable(record.color)));
|
createContactAvatarAttachment(record.number), Optional.fromNullable(record.color)));
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
out.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (contactsFile.exists() && contactsFile.length() > 0) {
|
if (contactsFile.exists() && contactsFile.length() > 0) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue