KeyManagementException, NoSuchAlgorithmException, APPCException {
/* Point to the certificate */
- FileInputStream fs = new FileInputStream(certFile);
+ try(FileInputStream fs = new FileInputStream(certFile)) {
- /* Generate a certificate from the X509 */
- CertificateFactory cf = CertificateFactory.getInstance("X.509");
- X509Certificate cert = (X509Certificate) cf.generateCertificate(fs);
+ /* Generate a certificate from the X509 */
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert = (X509Certificate) cf.generateCertificate(fs);
- /* Create a keystore object and load the certificate there */
- KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
- keystore.load(null, null);
- keystore.setCertificateEntry("cacert", cert);
+ /* Create a keystore object and load the certificate there */
+ KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
+ keystore.load(null, null);
+ keystore.setCertificateEntry("cacert", cert);
- SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(keystore).build();
- SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslcontext,
- SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
+ SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(keystore).build();
+ SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslcontext,
+ SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
- httpClient = HttpClients.custom().setSSLSocketFactory(factory).build();
+ httpClient = HttpClients.custom().setSSLSocketFactory(factory).build();
+ }
}
/**
// write to it. If it does not exist, don't write to it.
File tmpFile = new File(fileName);
if (tmpFile.exists()) {
- BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true));
- // out.write(dataToWrite);
- // out.write(getTheDate() +": " +Thread.currentThread().getName() +": "+dataToWrite);
- out.write(getTheDate() + ": " + tId + ": " + dataToWrite);
- out.close();
+ try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) {
+ // out.write(dataToWrite);
+ // out.write(getTheDate() +": " +Thread.currentThread().getName() +": "+dataToWrite);
+ out.write(getTheDate() + ": " + tId + ": " + dataToWrite);
+ }
}
} catch (IOException e) {
debugLog.printRTAriDebug(fn, "Caught an IOException: e=" + e);
// write to it. If it does not exist, don't write to it.
File tmpFile = new File(fileName);
if (tmpFile.exists()) {
- BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true));
- out.write(dataToWrite);
- out.close();
+ try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) {
+ out.write(dataToWrite);
+ }
}
} catch (IOException e) {
debugLog.printRTAriDebug(fn, "Caught an IOException: e=" + e);
{
// if ((tmpFile.exists()) && (tmpFile.setWritable(true, true)))
if (tmpFile.exists()) {
- BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true));
- // out.write("<!-- "+getTheDate() +": " +tId +" -->\n");
- out.write(dataToWrite.toString());
- out.close();
+ try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) {
+ // out.write("<!-- "+getTheDate() +": " +tId +" -->\n");
+ out.write(dataToWrite.toString());
+ }
}
}
} catch (IOException e) {
File tmpFile = new File(fileName);
// if ((tmpFile.exists()) && (tmpFile.setWritable(true, true)))
if (tmpFile.exists()) {
- BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true));
- // out.write("<!-- "+getTheDate() +": " +tId +" -->\n");
- out.write(charBuffer, 0, len);
- out.close();
+ try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) {
+ // out.write("<!-- "+getTheDate() +": " +tId +" -->\n");
+ out.write(charBuffer, 0, len);
+ }
}
} catch (IOException e) {
System.err.println("writeToFile() exception: " + e);
public String getLastFewLinesOfFile(File file, int linesToRead) throws IOException {
String fn = "SshJcraftWrapper.getLastFewLinesOfFile";
- RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
- int lines = 0;
- StringBuilder builder = new StringBuilder();
String tail = "";
- long length = file.length();
- length--;
- randomAccessFile.seek(length);
- for (long seek = length; seek >= 0; --seek) {
- randomAccessFile.seek(seek);
- char c = (char) randomAccessFile.read();
- builder.append(c);
- if (c == '\n') {
- builder = builder.reverse();
- // System.out.println(builder.toString());
- tail = builder.toString() + tail;
- lines++;
- builder.setLength(0);
- if (lines == linesToRead) {
- break;
+ try(RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
+ int lines = 0;
+ StringBuilder builder = new StringBuilder();
+ long length = file.length();
+ length--;
+ randomAccessFile.seek(length);
+ for (long seek = length; seek >= 0; --seek) {
+ randomAccessFile.seek(seek);
+ char c = (char) randomAccessFile.read();
+ builder.append(c);
+ if (c == '\n') {
+ builder = builder.reverse();
+ // System.out.println(builder.toString());
+ tail = builder.toString() + tail;
+ lines++;
+ builder.setLength(0);
+ if (lines == linesToRead) {
+ break;
+ }
}
}
}
- randomAccessFile.close();
if (!jcraftReadSwConfigFileFromDisk()) {
debugLog.printRTAriDebug(fn, "tail='" + tail + "'");
}