Skip to content
Permalink
Browse files
Fixed SonarQube findings ...
  • Loading branch information
chrisdutz committed Dec 18, 2016
1 parent 4463329 commit 790b355688dc0ada8c62cb1df91faf67f52126ea
Showing 4 changed files with 174 additions and 92 deletions.
@@ -358,8 +358,9 @@ private String getAirVersion(File rootDirectory) throws ConverterException {
}

DataInputStream in = null;
FileInputStream descriptorInputStream = null;
try {
final FileInputStream descriptorInputStream = new FileInputStream(sdkDescriptor);
descriptorInputStream = new FileInputStream(sdkDescriptor);
in = new DataInputStream(descriptorInputStream);
final BufferedReader br = new BufferedReader(new InputStreamReader(in));
final String strLine = br.readLine();
@@ -374,6 +375,13 @@ private String getAirVersion(File rootDirectory) throws ConverterException {
// Ignore.
}
}
if (descriptorInputStream != null) {
try {
descriptorInputStream.close();
} catch (IOException e) {
// Ignore.
}
}
}
}

@@ -95,10 +95,11 @@ protected void processArchive() throws ConverterException {
protected String calculateChecksum(File jarFile) throws ConverterException {
// Implement the calculation of checksums for a given jar.
final MessageDigest digest;
InputStream is = null;
try {
digest = MessageDigest.getInstance("SHA-1");

final InputStream is = new FileInputStream(jarFile);
is = new FileInputStream(jarFile);
final byte[] buffer = new byte[8192];
int read;
try {
@@ -110,21 +111,22 @@ protected String calculateChecksum(File jarFile) throws ConverterException {
return bigInt.toString(16);
}
catch(IOException e) {
throw new RuntimeException("Unable to process file for MD5", e);
}
finally {
try {
is.close();
}
catch(IOException e) {
//noinspection ThrowFromFinallyBlock
throw new RuntimeException("Unable to close input stream for MD5 calculation", e);
}
throw new ConverterException("Unable to process file for MD5", e);
}
} catch (NoSuchAlgorithmException e) {
throw new ConverterException("Error calculating checksum of file '" + jarFile.getPath() + "'", e);
} catch (FileNotFoundException e) {
throw new ConverterException("Error calculating checksum of file '" + jarFile.getPath() + "'", e);
} finally {
try {
if(is != null) {
is.close();
}
}
catch(IOException e) {
//noinspection ThrowFromFinallyBlock
throw new ConverterException("Unable to close input stream for MD5 calculation", e);
}
}
}

@@ -142,10 +144,17 @@ protected MavenArtifact lookupMetadataForChecksum(String checksum) throws Conver
} else {
connection = queryUrl.openConnection();
}
final ReadableByteChannel rbc = Channels.newChannel(connection.getInputStream());
final ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
if(rbc.read(byteBuffer) > 0) {
output = new String (byteBuffer.array(), "UTF-8");
ReadableByteChannel rbc = null;
try {
rbc = Channels.newChannel(connection.getInputStream());
final ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
if (rbc.read(byteBuffer) > 0) {
output = new String(byteBuffer.array(), "UTF-8");
}
} finally {
if(rbc != null) {
rbc.close();
}
}
} catch (MalformedURLException e) {
throw new ConverterException("Error querying maven central.", e);
@@ -45,9 +45,11 @@ protected void unpack(File inputArchive, File targetDirectory) throws RetrieverE

ArchiveInputStream archiveInputStream = null;
ArchiveEntry entry;
FileInputStream fileInputStream = null;
try {

final CountingInputStream inputStream = new CountingInputStream(new FileInputStream(inputArchive));
fileInputStream = new FileInputStream(inputArchive);
final CountingInputStream inputStream = new CountingInputStream(fileInputStream);

final long inputFileSize = inputArchive.length();

@@ -76,20 +78,28 @@ protected void unpack(File inputArchive, File targetDirectory) throws RetrieverE
// Entry is a file.
else {
final byte[] data = new byte[BUFFER_MAX];
final FileOutputStream fos = new FileOutputStream(outputFile);
BufferedOutputStream dest = null;
FileOutputStream fos = null;
try {
dest = new BufferedOutputStream(fos, BUFFER_MAX);
fos = new FileOutputStream(outputFile);

int count;
while ((count = archiveInputStream.read(data, 0, BUFFER_MAX)) != -1) {
dest.write(data, 0, count);
progressBar.updateProgress(inputStream.getBytesRead());
BufferedOutputStream dest = null;
try {
dest = new BufferedOutputStream(fos, BUFFER_MAX);

int count;
while ((count = archiveInputStream.read(data, 0, BUFFER_MAX)) != -1) {
dest.write(data, 0, count);
progressBar.updateProgress(inputStream.getBytesRead());
}
} finally {
if (dest != null) {
dest.flush();
dest.close();
}
}
} finally {
if(dest != null) {
dest.flush();
dest.close();
if(fos != null) {
fos.close();
}
}
}
@@ -106,7 +116,14 @@ protected void unpack(File inputArchive, File targetDirectory) throws RetrieverE
if(archiveInputStream != null) {
try {
archiveInputStream.close();
} catch(Exception e) {
} catch(IOException e) {
// Ignore...
}
}
if(fileInputStream != null) {
try {
fileInputStream.close();
} catch(IOException e) {
// Ignore...
}
}
@@ -27,9 +27,9 @@
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.w3c.dom.Document;
@@ -189,29 +189,47 @@ protected void performFastDownload(URL sourceUrl, File targetFile) throws IOExce
} else {
connection = sourceUrl.openConnection();
}
final ReadableByteChannel rbc = Channels.newChannel(connection.getInputStream());
final FileOutputStream fos = new FileOutputStream(targetFile);
ReadableByteChannel rbc = null;
FileOutputStream fos = null;
try {
rbc = Channels.newChannel(connection.getInputStream());
fos = new FileOutputStream(targetFile);

////////////////////////////////////////////////////////////////////////////////
// Do the downloading.
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Do the downloading.
////////////////////////////////////////////////////////////////////////////////

final long expectedSize = connection.getContentLength();
long transferedSize = 0L;
final long expectedSize = connection.getContentLength();
long transferedSize = 0L;

System.out.println("===========================================================");
System.out.println("Downloading " + sourceUrl.toString());
if(expectedSize > 1014 * 1024) {
System.out.println("Expected size: " + (expectedSize / 1024 / 1024) + "MB");
} else {
System.out.println("Expected size: " + (expectedSize / 1024 ) + "KB");
}
final ProgressBar progressBar = new ProgressBar(expectedSize);
while (transferedSize < expectedSize) {
transferedSize += fos.getChannel().transferFrom(rbc, transferedSize, 1 << 20);
progressBar.updateProgress(transferedSize);
System.out.println("===========================================================");
System.out.println("Downloading " + sourceUrl.toString());
if (expectedSize > 1014 * 1024) {
System.out.println("Expected size: " + (expectedSize / 1024 / 1024) + "MB");
} else {
System.out.println("Expected size: " + (expectedSize / 1024) + "KB");
}
final ProgressBar progressBar = new ProgressBar(expectedSize);
while (transferedSize < expectedSize) {
transferedSize += fos.getChannel().transferFrom(rbc, transferedSize, 1 << 20);
progressBar.updateProgress(transferedSize);
}
} finally {
if(rbc != null) {
try {
rbc.close();
} catch (IOException e) {
// Ignore ...
}
}
if(fos != null) {
try {
fos.close();
} catch (IOException e) {
// Ignore ...
}
}
}
fos.close();
System.out.println();
System.out.println("Finished downloading.");
System.out.println("===========================================================");
@@ -227,59 +245,89 @@ protected void performSafeDownload(URI sourceUri, File targetFile) throws IOExce
config = RequestConfig.DEFAULT;
}

HttpGet httpget = new HttpGet(sourceUri);
httpget.setConfig(config);
HttpClient httpclient = HttpClients.createDefault();
HttpResponse response = httpclient.execute(httpget);

String reasonPhrase = response.getStatusLine().getReasonPhrase();
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(String.format("statusCode: %d", statusCode));
System.out.println(String.format("reasonPhrase: %s", reasonPhrase));

HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
CloseableHttpClient httpclient = null;
try {
HttpGet httpget = new HttpGet(sourceUri);
httpget.setConfig(config);
httpclient = HttpClients.createDefault();
HttpResponse response = httpclient.execute(httpget);

final ReadableByteChannel rbc = Channels.newChannel(content);
final FileOutputStream fos = new FileOutputStream(targetFile);
String reasonPhrase = response.getStatusLine().getReasonPhrase();
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(String.format("statusCode: %d", statusCode));
System.out.println(String.format("reasonPhrase: %s", reasonPhrase));

////////////////////////////////////////////////////////////////////////////////
// Do the downloading.
////////////////////////////////////////////////////////////////////////////////
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();

final long expectedSize = entity.getContentLength();
System.out.println("===========================================================");
System.out.println("Downloading " + sourceUri.toString());
if(expectedSize <= 0) {
ReadableByteChannel rbc = null;
FileOutputStream fos = null;
try {
System.out.println("Unknown size.");
IOUtils.copy(content, fos);
rbc = Channels.newChannel(content);
fos = new FileOutputStream(targetFile);

////////////////////////////////////////////////////////////////////////////////
// Do the downloading.
////////////////////////////////////////////////////////////////////////////////

final long expectedSize = entity.getContentLength();
System.out.println("===========================================================");
System.out.println("Downloading " + sourceUri.toString());
if (expectedSize <= 0) {
try {
System.out.println("Unknown size.");
IOUtils.copy(content, fos);
} finally {
// close http network connection
content.close();
}
} else {
if (expectedSize > 1014 * 1024) {
System.out.println("Expected size: " + (expectedSize / 1024 / 1024) + "MB");
} else {
System.out.println("Expected size: " + (expectedSize / 1024) + "KB");
}
final ProgressBar progressBar = new ProgressBar(expectedSize);
long transferredSize = 0L;
while ((expectedSize == 0) || (transferredSize < expectedSize)) {
// Transfer about 1MB in each iteration.
long currentSize = fos.getChannel().transferFrom(rbc, transferredSize, MEGABYTE);
if (currentSize < MEGABYTE) {
break;
}
transferredSize += currentSize;
progressBar.updateProgress(transferredSize);
}
fos.close();
System.out.println();
}
System.out.println("Finished downloading.");
System.out.println("===========================================================");
} finally {
// close http network connection
content.close();
}
} else {
if (expectedSize > 1014 * 1024) {
System.out.println("Expected size: " + (expectedSize / 1024 / 1024) + "MB");
} else {
System.out.println("Expected size: " + (expectedSize / 1024) + "KB");
if(rbc != null) {
try {
rbc.close();
} catch (IOException e) {
// Ignore ...
}
}
if(fos != null) {
try {
fos.close();
} catch (IOException e) {
// Ignore ...
}
}
}
final ProgressBar progressBar = new ProgressBar(expectedSize);
long transferredSize = 0L;
while ((expectedSize == 0) || (transferredSize < expectedSize)) {
// Transfer about 1MB in each iteration.
long currentSize = fos.getChannel().transferFrom(rbc, transferredSize, MEGABYTE);
if(currentSize < MEGABYTE) {
break;
} finally {
if(httpclient != null) {
try {
httpclient.close();
} catch(IOException e) {
// Ignore ...
}
transferredSize += currentSize;
progressBar.updateProgress(transferredSize);
}
fos.close();
System.out.println();
}
System.out.println("Finished downloading.");
System.out.println("===========================================================");
}

protected String getBinaryUrl(SdkType sdkType, String version, PlatformType platformType)

0 comments on commit 790b355

Please sign in to comment.