Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unnecessary modifier :: variables declared in try-with-resources are … #1026

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/emissary/config/ExtractResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public String getResource(final String theResource) throws IOException {
}
logger.debug("Reading " + resource);
final String result;
try (final InputStream is = ConfigUtil.getConfigStream(resource);
final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
try (InputStream is = ConfigUtil.getConfigStream(resource);
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
final byte[] buf = new byte[4096];
int thisReadOp = 0;
while ((thisReadOp = is.read(buf)) > -1) {
Expand All @@ -63,7 +63,7 @@ public void writeResource(final String theResource) throws IOException {
final String rezdata = getResource(resource);
final String outputPath = this.outputDirectory + "/" + resource.replaceAll("/", ".");
logger.debug("Writing " + outputPath);
try (final BufferedOutputStream os = new BufferedOutputStream(Files.newOutputStream(Paths.get(outputPath)))) {
try (BufferedOutputStream os = new BufferedOutputStream(Files.newOutputStream(Paths.get(outputPath)))) {
os.write(rezdata.getBytes());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/emissary/core/BaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ public long getChannelSize() throws IOException {
case BYTE_ARRAY_ONLY:
return ArrayUtils.getLength(theData);
case CHANNEL_ONLY:
try (final SeekableByteChannel sbc = this.seekableByteChannelFactory.create()) {
try (SeekableByteChannel sbc = this.seekableByteChannelFactory.create()) {
return sbc.size();
}
case NO_DATA:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected final int readImpl(final ByteBuffer byteBuffer) throws IOException {
@Override
protected long sizeImpl() throws IOException {
if (size < 0) {
try (final InputStream is = inputStreamFactory.create()) {
try (InputStream is = inputStreamFactory.create()) {
size = SeekableByteChannelHelper.available(is);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static SeekableByteChannelFactory inputStream(final long size, final Inpu
* @return a byte array of the data from the BDO sized up to maxSize (so could truncate data)
*/
public static byte[] getByteArrayFromBdo(final IBaseDataObject ibdo, final int maxSize) {
try (final SeekableByteChannel sbc = ibdo.getChannelFactory().create()) {
try (SeekableByteChannel sbc = ibdo.getChannelFactory().create()) {
final long truncatedBy = sbc.size() - maxSize;
if (truncatedBy > 0 && logger.isWarnEnabled()) {
logger.warn("Returned data for [{}] will be truncated by {} bytes due to size constraints of byte arrays", ibdo.shortName(),
Expand All @@ -108,7 +108,7 @@ public static byte[] getByteArrayFromBdo(final IBaseDataObject ibdo, final int m
* @throws IOException if we couldn't read all the data
*/
public static byte[] getByteArrayFromChannel(final SeekableByteChannelFactory sbcf, final int maxSize) throws IOException {
try (final SeekableByteChannel sbc = sbcf.create()) {
try (SeekableByteChannel sbc = sbcf.create()) {
final int byteArraySize = (int) Math.min(sbc.size(), maxSize);
final ByteBuffer buff = ByteBuffer.allocate(byteArraySize);

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/emissary/jni/JNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ public boolean retrieveFile(final String filename, final String[] errmsg) {
}

// Save the contents into the disk file
try (final FileOutputStream fos = new FileOutputStream(fullPathName);
final BufferedOutputStream bos = new BufferedOutputStream(fos)) {
try (FileOutputStream fos = new FileOutputStream(fullPathName);
BufferedOutputStream bos = new BufferedOutputStream(fos)) {
bos.write(libContents, 0, libContents.length);
} catch (IOException ioe) {
errmsg[0] = "Cannot write retrieved JNI library to " + fullPathName + ": " + ioe;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/emissary/jni/JniRepositoryPlace.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ private void configurePlace() {
return null;
}

try (final InputStream theFile = Files.newInputStream(nativeLib.toPath());
final DataInputStream theStream = new DataInputStream(theFile)) {
try (InputStream theFile = Files.newInputStream(nativeLib.toPath());
DataInputStream theStream = new DataInputStream(theFile)) {
final byte[] theContent = new byte[theStream.available()];
theStream.readFully(theContent);
return theContent;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/emissary/kff/ChecksumCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public ChecksumResults digest(final SeekableByteChannelFactory sbcf) {
final byte[] b = new byte[1024];

for (final MessageDigest d : digest) {
try (final InputStream is = Channels.newInputStream(sbcf.create())) {
try (InputStream is = Channels.newInputStream(sbcf.create())) {
d.reset();

int bytesRead;
Expand All @@ -194,7 +194,7 @@ public ChecksumResults digest(final SeekableByteChannelFactory sbcf) {
}

if (crc != null) {
try (final InputStream is = Channels.newInputStream(sbcf.create())) {
try (InputStream is = Channels.newInputStream(sbcf.create())) {
crc.reset();

int bytesRead;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/emissary/kff/KffChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public KffResult check(final String itemName, final SeekableByteChannelFactory s
final ChecksumResults sums = computeSums(sbcf);
KffResult answer = null;
long sbcSize = 0;
try (final SeekableByteChannel sbc = sbcf.create()) {
try (SeekableByteChannel sbc = sbcf.create()) {
sbcSize = sbc.size();
}
if (sbcSize < kffMinDataSize || list.isEmpty()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/emissary/kff/KffDataObjectHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public Map<String, String> hashData(final SeekableByteChannelFactory sbcf, final

KffResult kffCheck = null;
if (sbcf != null) {
try (final SeekableByteChannel sbc = sbcf.create()) {
try (SeekableByteChannel sbc = sbcf.create()) {
if (sbc.size() > 0) {
kffCheck = kff.check(name, sbcf);
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/emissary/kff/Ssdeep.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public SsContext(@Nullable final byte[] data) {
public SsContext(final SeekableByteChannelFactory sbcf) {
long expectedInputLength = 0;

try (final SeekableByteChannel sbc = sbcf.create()) {
try (SeekableByteChannel sbc = sbcf.create()) {
expectedInputLength = sbc.size();
} catch (final IOException ioe) {
// Ignore
Expand Down Expand Up @@ -283,7 +283,7 @@ public SpamSumSignature generateHash(final SeekableByteChannelFactory sbcf) {
beginHashing();
final RollingState rollState = new RollingState();

try (final InputStream is = Channels.newInputStream(sbcf.create())) {
try (InputStream is = Channels.newInputStream(sbcf.create())) {
final byte[] b = new byte[1024];

int bytesRead;
Expand Down Expand Up @@ -438,7 +438,7 @@ public String fuzzyHash(final SeekableByteChannelFactory sbcf) {
* @throws IOException If there is some I/O problem accessing the file.
*/
public String fuzzyHashFile(final File file) throws IOException {
try (final RandomAccessFile stream = new RandomAccessFile(file, "r")) {
try (RandomAccessFile stream = new RandomAccessFile(file, "r")) {
final SsContext ctx = new SsContext(file);
while (true) {
stream.seek(0);
Expand Down Expand Up @@ -706,7 +706,7 @@ public int compare(@Nullable final SpamSumSignature signature1, @Nullable final
public static void main(final String[] args) throws Exception {
final Ssdeep ss = new Ssdeep();
for (final String f : args) {
try (final InputStream is = Files.newInputStream(Paths.get(f))) {
try (InputStream is = Files.newInputStream(Paths.get(f))) {
final byte[] buffer = IOUtils.toByteArray(is);
// output format matches the original ssdeep program
System.out.println(ss.fuzzyHash(buffer) + ",\"" + f + "\"");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/emissary/server/EmissaryServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ private static SslContextFactory.Server getSslContextFactory() throws IOExceptio
sslContextFactory.setKeyStorePassword(keystorePass);

KeyStore trustStoreInstance;
try (final InputStream is = Files.newInputStream(Paths.get(trustStore))) {
try (InputStream is = Files.newInputStream(Paths.get(trustStore))) {
trustStoreInstance = KeyStore.getInstance("JKS");
trustStoreInstance.load(is, trustStorePass.toCharArray());
} catch (KeyStoreException | CertificateException | NoSuchAlgorithmException e) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/emissary/util/PkiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static KeyStore buildStore(@Nullable final String path, final char[] pazz
if (isPemCertificate(pemData)) {
loadKeyStore(keyStore, pemData);
} else {
try (final InputStream is = Files.newInputStream(Paths.get(path))) {
try (InputStream is = Files.newInputStream(Paths.get(path))) {
keyStore.load(is, pazz);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/emissary/util/shell/Executrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ public static boolean writeDataToFile(@Nullable final byte[] theContent, final S
*/
public static void writeFile(final byte[] theContent, final int pos, final int len, final String filename, final boolean append)
throws IOException {
try (final FileOutputStream theOutput = new FileOutputStream(filename, append);
final BufferedOutputStream theStream = new BufferedOutputStream(theOutput)) {
try (FileOutputStream theOutput = new FileOutputStream(filename, append);
BufferedOutputStream theStream = new BufferedOutputStream(theOutput)) {
theStream.write(theContent, pos, len);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/emissary/util/xml/AbstractJDOMUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected static Document createDocument(final InputSource is, @Nullable final X
@Nullable
public static String toString(final Document jdom) {
final XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
outputter.output(jdom, os);
return os.toString();
} catch (IOException iox) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/emissary/config/ServiceConfigGuideTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ void testConstructors() throws IOException {
// Write the config bytes out to a temp file
final File scfile = File.createTempFile("temp", ".cfg");
scfile.deleteOnExit();
try (final OutputStream os = Files.newOutputStream(scfile.toPath())) {
try (OutputStream os = Files.newOutputStream(scfile.toPath())) {
os.write(cdata.getBytes());
}

Expand Down
12 changes: 6 additions & 6 deletions src/test/java/emissary/core/BaseDataObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void testExceptionWhenGettingChannelFactory() throws IOException {
// Hook into the SBCF
this.b.setChannelFactory(sbcf);
// Hook into an SBC
try (final SeekableByteChannel sbc = Mockito.spy(this.b.getChannelFactory().create())) {
try (SeekableByteChannel sbc = Mockito.spy(this.b.getChannelFactory().create())) {
// Always return this spied SBC
Mockito.when(sbcf.create()).thenReturn(sbc);
// Kick an exception when calling size()
Expand All @@ -183,7 +183,7 @@ void testExceptionWhenGettingChannelSize() throws IOException {
final SeekableByteChannelFactory sbcf = Mockito.spy(SeekableByteChannelHelper.memory("Test data".getBytes()));
this.b.setChannelFactory(sbcf);
// Hook into an SBC
try (final SeekableByteChannel sbc = Mockito.spy(this.b.getChannelFactory().create())) {
try (SeekableByteChannel sbc = Mockito.spy(this.b.getChannelFactory().create())) {
// Always return this spied SBC
Mockito.when(sbcf.create()).thenReturn(sbc);
// Kick an exception when asking for the size
Expand Down Expand Up @@ -1314,8 +1314,8 @@ void testNewInputStream() throws IOException {

ibdo.setData(bytes1);

try (final InputStream bytesInputStream = ibdo.newInputStream();
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();) {
try (InputStream bytesInputStream = ibdo.newInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();) {
IOUtils.copy(bytesInputStream, byteArrayOutputStream);

assertArrayEquals(bytes1, byteArrayOutputStream.toByteArray());
Expand All @@ -1326,8 +1326,8 @@ void testNewInputStream() throws IOException {

ibdo.setChannelFactory(sbcf);

try (final InputStream sbcfInputStream = ibdo.newInputStream();
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
try (InputStream sbcfInputStream = ibdo.newInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
IOUtils.copy(sbcfInputStream, byteArrayOutputStream);

assertArrayEquals(bytes2, byteArrayOutputStream.toByteArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ChannelTestHelper {

public static void checkByteArrayAgainstSbc(final byte[] bytesToVerify, final SeekableByteChannelFactory sbcf)
throws IOException {
try (final SeekableByteChannel sbc = sbcf.create()) {
try (SeekableByteChannel sbc = sbcf.create()) {
int startIndex;
int length;
for (startIndex = 0; startIndex < bytesToVerify.length; startIndex++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public SeekableByteChannel create() {
};
final SeekableByteChannelFactory sbcf = ImmutableChannelFactory.create(simbcf);
final ByteBuffer buff = ByteBuffer.wrap(TEST_STRING.concat(TEST_STRING).getBytes());
try (final SeekableByteChannel sbc = sbcf.create()) {
try (SeekableByteChannel sbc = sbcf.create()) {
assertThrows(NonWritableChannelException.class, () -> sbc.write(buff), "Writes aren't allowed to immutable channels");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void testRead() throws IOException {
@Test
void testSize() throws IOException {
// Normal path
try (final SeekableByteChannel sbc = InputStreamChannelFactory.create(testBytes.length, new TestInputStreamFactory(testBytes)).create()) {
try (SeekableByteChannel sbc = InputStreamChannelFactory.create(testBytes.length, new TestInputStreamFactory(testBytes)).create()) {
ByteBuffer buff = ByteBuffer.allocate(32);
assertEquals(testBytes.length, sbc.size());
assertEquals(testBytes.length, sbc.read(buff));
Expand All @@ -99,7 +99,7 @@ void testSize() throws IOException {
@Test
void testCanWorkOutSize() throws IOException {
// Make the factory work it out
try (final SeekableByteChannel sbc = InputStreamChannelFactory.create(-1, new TestInputStreamFactory(testBytes)).create()) {
try (SeekableByteChannel sbc = InputStreamChannelFactory.create(-1, new TestInputStreamFactory(testBytes)).create()) {
ByteBuffer buff = ByteBuffer.allocate(32);
assertEquals(testBytes.length, sbc.size());
assertEquals(testBytes.length, sbc.read(buff));
Expand All @@ -111,7 +111,7 @@ void testCanWorkOutSize() throws IOException {
void testReadWithIncorrectSize() throws IOException {
// You can set the size incorrectly, but this is still 'valid'
final int sbcLength = testBytes.length + 8;
try (final SeekableByteChannel sbc = InputStreamChannelFactory.create(sbcLength, new TestInputStreamFactory(testBytes)).create()) {
try (SeekableByteChannel sbc = InputStreamChannelFactory.create(sbcLength, new TestInputStreamFactory(testBytes)).create()) {
ByteBuffer buff = ByteBuffer.allocate(32);
assertEquals(sbcLength, sbc.size());
assertEquals(testBytes.length, sbc.read(buff));
Expand All @@ -122,7 +122,7 @@ void testReadWithIncorrectSize() throws IOException {
@Test
void testReadWithZeroSize() throws IOException {
// Zero length channel - will always be EOS, not reading anything into the buffer
try (final SeekableByteChannel sbc = InputStreamChannelFactory.create(0, new TestInputStreamFactory(testBytes)).create()) {
try (SeekableByteChannel sbc = InputStreamChannelFactory.create(0, new TestInputStreamFactory(testBytes)).create()) {
ByteBuffer buff = ByteBuffer.allocate(1);
assertEquals(0, sbc.size());
assertEquals(-1, sbc.read(buff));
Expand All @@ -134,7 +134,7 @@ void testReadWithZeroSize() throws IOException {
void testStartReadBeyondActualData() throws IOException {
// Set an SBC that is larger than the data we have, ensure an IOException occurs when reading
final int sbcLength = testBytes.length + 8;
try (final SeekableByteChannel sbc = InputStreamChannelFactory.create(sbcLength, new TestInputStreamFactory(testBytes)).create()) {
try (SeekableByteChannel sbc = InputStreamChannelFactory.create(sbcLength, new TestInputStreamFactory(testBytes)).create()) {
ByteBuffer buff = ByteBuffer.allocate(32);
assertEquals(sbcLength, sbc.size());
// Set position beyond length
Expand All @@ -148,7 +148,7 @@ void testStartReadBeyondActualData() throws IOException {
void testReadWithLargerThanDefinedSize() throws IOException {
final int sbcLength = testBytes.length / 2;
// Set an SBC that is smaller than the amount of data we have, ensure that we can't read more than the defined size
try (final SeekableByteChannel sbc = InputStreamChannelFactory.create(sbcLength, new TestInputStreamFactory(testBytes)).create()) {
try (SeekableByteChannel sbc = InputStreamChannelFactory.create(sbcLength, new TestInputStreamFactory(testBytes)).create()) {
ByteBuffer buff = ByteBuffer.allocate(32);
assertEquals(sbcLength, sbc.size());
sbc.read(buff);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void testGetJournalEntries() throws IOException, InterruptedException {
}
final ArrayList<JournalEntry> result = new ArrayList<>();
for (final Path journalPath : JournalReader.getJournalPaths(this.directory)) {
try (final JournalReader jr = new JournalReader(journalPath)) {
try (JournalReader jr = new JournalReader(journalPath)) {
Journal j = jr.getJournal();
result.addAll(j.getEntries());
}
Expand Down
Loading
Loading