Skip to content

Commit

Permalink
use try-with-resource and java.io.InputStream convenience methods
Browse files Browse the repository at this point in the history
  • Loading branch information
EcljpseB0T authored and HannesWell committed Jun 22, 2023
1 parent 70efe52 commit 8983be4
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 272 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public abstract class PlatformURLConnection extends URLConnection {
private static String filePrefix;

// constants
private static final int BUF_SIZE = 32768;
private static final Object NOT_FOUND = new Object(); // marker
private static final String CACHE_PROP = ".cache.properties"; //$NON-NLS-1$
private static final String CACHE_LOCATION_PROP = "location"; //$NON-NLS-1$
Expand Down Expand Up @@ -148,7 +147,6 @@ private void copyToCache() throws IOException {
src = new URL(tmp);
} else
src = resolvedURL;
InputStream srcis = null;

// cache target
String tgt;
Expand All @@ -163,40 +161,23 @@ private void copyToCache() throws IOException {
tgt = tmp;
} else
tgt = cachedURL.getFile();
File tgtFile = null;
FileOutputStream tgtos = null;

boolean error = false;
long total = 0;

try {
try (InputStream srcis = src.openStream()) {
if (DEBUG && DEBUG_CACHE_COPY) {
if (isJar)
debug("Caching jar as " + tgt); //$NON-NLS-1$
else
debug("Caching as " + tgt); //$NON-NLS-1$
}

srcis = src.openStream();
byte[] buf = new byte[BUF_SIZE];
int count = srcis.read(buf);

tgtFile = new File(tgt);
tgtos = new FileOutputStream(tgtFile);

while (count != -1) {
total += count;
tgtos.write(buf, 0, count);
count = srcis.read(buf);
File tgtFile = new File(tgt);
try (FileOutputStream tgtos = new FileOutputStream(tgtFile)) {
srcis.transferTo(tgtos);
tgtos.flush();
tgtos.getFD().sync();
}

srcis.close();
srcis = null;
tgtos.flush();
tgtos.getFD().sync();
tgtos.close();
tgtos = null;

// add cache entry
cacheIndex.put(key, tgt);
isInCache = true;
Expand All @@ -210,10 +191,6 @@ private void copyToCache() throws IOException {
} finally {
if (!error && DEBUG && DEBUG_CACHE_COPY)
debug(total + " bytes copied"); //$NON-NLS-1$
if (srcis != null)
srcis.close();
if (tgtos != null)
tgtos.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,39 +508,31 @@ private String getLibraryFromFragment(String fragment) {
if (frag.isDirectory())
return searchFor("eclipse", fragment); //$NON-NLS-1$;

ZipFile fragmentJar = null;
try {
fragmentJar = new ZipFile(frag);
} catch (IOException e) {
log("Exception opening JAR file: " + fragment); //$NON-NLS-1$
log(e);
return null;
}

Enumeration<? extends ZipEntry> entries = fragmentJar.entries();
String entry = null;
while (entries.hasMoreElements()) {
ZipEntry zipEntry = entries.nextElement();
if (zipEntry.getName().startsWith("eclipse_")) { //$NON-NLS-1$
entry = zipEntry.getName();
try {
fragmentJar.close();
} catch (IOException e) {
//ignore
try (ZipFile fragmentJar = new ZipFile(frag)) {
Enumeration<? extends ZipEntry> entries = fragmentJar.entries();
String entry = null;
while (entries.hasMoreElements()) {
ZipEntry zipEntry = entries.nextElement();
if (zipEntry.getName().startsWith("eclipse_")) { //$NON-NLS-1$
entry = zipEntry.getName();
break;
}
break;
}
}
if (entry != null) {
String lib = extractFromJAR(fragment, entry);
if (!getOS().equals("win32")) { //$NON-NLS-1$
try {
Runtime.getRuntime().exec(new String[] {"chmod", "755", lib}).waitFor(); //$NON-NLS-1$ //$NON-NLS-2$
} catch (Throwable e) {
//ignore
if (entry != null) {
String lib = extractFromJAR(fragment, entry);
if (!getOS().equals("win32")) { //$NON-NLS-1$
try {
Runtime.getRuntime().exec(new String[] {"chmod", "755", lib}).waitFor(); //$NON-NLS-1$ //$NON-NLS-2$
} catch (Throwable e) {
//ignore
}
}
return lib;
}
return lib;
} catch (IOException e) {
log("Exception opening JAR file: " + fragment); //$NON-NLS-1$
log(e);
return null;
}
return null;
}
Expand Down Expand Up @@ -1342,8 +1334,8 @@ private String computeDefaultUserAreaLocation(String pathAppendage) {
File eclipseProduct = new File(installDir, PRODUCT_SITE_MARKER);
if (eclipseProduct.exists()) {
Properties props = new Properties();
try {
props.load(new FileInputStream(eclipseProduct));
try (FileInputStream inStream = new FileInputStream(eclipseProduct)) {
props.load(inStream);
String appId = props.getProperty(PRODUCT_SITE_ID);
if (appId == null || appId.trim().length() == 0)
appId = ECLIPSE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,42 +156,42 @@ private void saveExtensionRegistry(long timestamp) throws IOException {
}

private void saveContributions(KeyedHashSet[] contributions) throws IOException {
FileOutputStream fosNamespace = new FileOutputStream(contributionsFile);
DataOutputStream outputNamespace = new DataOutputStream(new BufferedOutputStream(fosNamespace));
KeyedElement[] newElements = contributions[0].elements();
KeyedElement[] formerElements = contributions[1].elements();

// get count of contributions that will be cached
int cacheSize = 0;
for (KeyedElement newElement : newElements) {
if (((Contribution) newElement).shouldPersist()) {
cacheSize++;
try (FileOutputStream fosNamespace = new FileOutputStream(contributionsFile);
DataOutputStream outputNamespace = new DataOutputStream(new BufferedOutputStream(fosNamespace))) {
KeyedElement[] newElements = contributions[0].elements();
KeyedElement[] formerElements = contributions[1].elements();

// get count of contributions that will be cached
int cacheSize = 0;
for (KeyedElement newElement : newElements) {
if (((Contribution) newElement).shouldPersist()) {
cacheSize++;
}
}
}
for (KeyedElement formerElement : formerElements) {
if (((Contribution) formerElement).shouldPersist()) {
cacheSize++;
for (KeyedElement formerElement : formerElements) {
if (((Contribution) formerElement).shouldPersist()) {
cacheSize++;
}
}
}
outputNamespace.writeInt(cacheSize);
outputNamespace.writeInt(cacheSize);

for (KeyedElement newElement : newElements) {
Contribution element = (Contribution) newElement;
if (element.shouldPersist()) {
writeStringOrNull(element.getContributorId(), outputNamespace);
saveArray(filterContributionChildren(element), outputNamespace);
for (KeyedElement newElement : newElements) {
Contribution element = (Contribution) newElement;
if (element.shouldPersist()) {
writeStringOrNull(element.getContributorId(), outputNamespace);
saveArray(filterContributionChildren(element), outputNamespace);
}
}
}
for (KeyedElement formerElement : formerElements) {
Contribution element = (Contribution) formerElement;
if (element.shouldPersist()) {
writeStringOrNull(element.getContributorId(), outputNamespace);
saveArray(filterContributionChildren(element), outputNamespace);
for (KeyedElement formerElement : formerElements) {
Contribution element = (Contribution) formerElement;
if (element.shouldPersist()) {
writeStringOrNull(element.getContributorId(), outputNamespace);
saveArray(filterContributionChildren(element), outputNamespace);
}
}
outputNamespace.flush();
fosNamespace.getFD().sync();
}
outputNamespace.flush();
fosNamespace.getFD().sync();
outputNamespace.close();
}

// Contribution has raw children in a unique format that combines extensions and extension points.
Expand All @@ -208,65 +208,70 @@ private int[] filterContributionChildren(Contribution element) {
}

private void saveNamespaces(KeyedHashSet namespacesIndex) throws IOException {
FileOutputStream fosNamespace = new FileOutputStream(namespacesFile);
DataOutputStream outputNamespace = new DataOutputStream(new BufferedOutputStream(fosNamespace));
KeyedElement[] elements = namespacesIndex.elements();

KeyedElement[] cachedElements = new KeyedElement[elements.length];
int cacheSize = 0;
for (KeyedElement e : elements) {
RegistryIndexElement element = (RegistryIndexElement) e;
int[] extensionPoints = filter(element.getExtensionPoints());
int[] extensions = filter(element.getExtensions());
if (extensionPoints.length == 0 && extensions.length == 0)
continue;
RegistryIndexElement cachedElement = new RegistryIndexElement((String) element.getKey(), extensionPoints, extensions);
cachedElements[cacheSize] = cachedElement;
cacheSize++;
}
try (FileOutputStream fosNamespace = new FileOutputStream(namespacesFile);
DataOutputStream outputNamespace = new DataOutputStream(new BufferedOutputStream(fosNamespace))) {
KeyedElement[] elements = namespacesIndex.elements();

KeyedElement[] cachedElements = new KeyedElement[elements.length];
int cacheSize = 0;
for (KeyedElement e : elements) {
RegistryIndexElement element = (RegistryIndexElement) e;
int[] extensionPoints = filter(element.getExtensionPoints());
int[] extensions = filter(element.getExtensions());
if (extensionPoints.length == 0 && extensions.length == 0)
continue;
RegistryIndexElement cachedElement = new RegistryIndexElement((String) element.getKey(),
extensionPoints, extensions);
cachedElements[cacheSize] = cachedElement;
cacheSize++;
}

outputNamespace.writeInt(cacheSize);
for (int i = 0; i < cacheSize; i++) {
RegistryIndexElement element = (RegistryIndexElement) cachedElements[i];
writeStringOrNull((String) element.getKey(), outputNamespace);
saveArray(element.getExtensionPoints(), outputNamespace); // it was pre-filtered as we counted the number of elements
saveArray(element.getExtensions(), outputNamespace); // it was pre-filtered as we counted the number of elements
outputNamespace.writeInt(cacheSize);
for (int i = 0; i < cacheSize; i++) {
RegistryIndexElement element = (RegistryIndexElement) cachedElements[i];
writeStringOrNull((String) element.getKey(), outputNamespace);
saveArray(element.getExtensionPoints(), outputNamespace); // it was pre-filtered as we counted the
// number of
// elements
saveArray(element.getExtensions(), outputNamespace); // it was pre-filtered as we counted the number of
// elements
}
outputNamespace.flush();
fosNamespace.getFD().sync();
}
outputNamespace.flush();
fosNamespace.getFD().sync();
outputNamespace.close();
}

private void saveContributors(HashMap<?, ?> contributors) throws IOException {
FileOutputStream fosContributors = new FileOutputStream(contributorsFile);
DataOutputStream outputContributors = new DataOutputStream(new BufferedOutputStream(fosContributors));

Collection<?> entries = contributors.values();
outputContributors.writeInt(entries.size());

for (Object entry : entries) {
RegistryContributor contributor = (RegistryContributor) entry;
writeStringOrNull(contributor.getActualId(), outputContributors);
writeStringOrNull(contributor.getActualName(), outputContributors);
writeStringOrNull(contributor.getId(), outputContributors);
writeStringOrNull(contributor.getName(), outputContributors);
}
try (FileOutputStream fosContributors = new FileOutputStream(contributorsFile);
DataOutputStream outputContributors = new DataOutputStream(new BufferedOutputStream(fosContributors))) {

Collection<?> entries = contributors.values();
outputContributors.writeInt(entries.size());

for (Object entry : entries) {
RegistryContributor contributor = (RegistryContributor) entry;
writeStringOrNull(contributor.getActualId(), outputContributors);
writeStringOrNull(contributor.getActualName(), outputContributors);
writeStringOrNull(contributor.getId(), outputContributors);
writeStringOrNull(contributor.getName(), outputContributors);
}

outputContributors.flush();
fosContributors.getFD().sync();
outputContributors.close();
outputContributors.flush();
fosContributors.getFD().sync();
outputContributors.close();
}
}

private void saveTables(long registryTimeStamp) throws IOException {
FileOutputStream fosTable = new FileOutputStream(tableFile);
DataOutputStream outputTable = new DataOutputStream(new BufferedOutputStream(fosTable));
writeCacheHeader(outputTable, registryTimeStamp);
outputTable.writeInt(objectManager.getNextId());
offsets.save(outputTable);
objectManager.getExtensionPoints().save(outputTable, objectManager); // uses writer to filter contents
outputTable.flush();
fosTable.getFD().sync();
outputTable.close();
try (FileOutputStream fosTable = new FileOutputStream(tableFile);
DataOutputStream outputTable = new DataOutputStream(new BufferedOutputStream(fosTable))) {
writeCacheHeader(outputTable, registryTimeStamp);
outputTable.writeInt(objectManager.getNextId());
offsets.save(outputTable);
objectManager.getExtensionPoints().save(outputTable, objectManager); // uses writer to filter contents
outputTable.flush();
fosTable.getFD().sync();
}
}

private void writeCacheHeader(DataOutputStream output, long registryTimeStamp) throws IOException {
Expand Down Expand Up @@ -444,21 +449,23 @@ private void saveOrphans() throws IOException {
if (filteredValue.length != 0)
filteredOrphans.put(entry.getKey(), filteredValue);
}
FileOutputStream fosOrphan = new FileOutputStream(orphansFile);
DataOutputStream outputOrphan = new DataOutputStream(new BufferedOutputStream(fosOrphan));
outputOrphan.writeInt(filteredOrphans.size());
Set<Entry<String, int[]>> elements = filteredOrphans.entrySet();
for (Entry<String, int[]> entry : elements) {
outputOrphan.writeUTF(entry.getKey());
saveArray(entry.getValue(), outputOrphan);
}
for (Entry<String, int[]> entry : elements) {
mainOutput.writeInt(entry.getValue().length);
saveExtensions((IExtension[]) objectManager.getHandles(entry.getValue(), RegistryObjectManager.EXTENSION), mainOutput);
try (FileOutputStream fosOrphan = new FileOutputStream(orphansFile);
DataOutputStream outputOrphan = new DataOutputStream(new BufferedOutputStream(fosOrphan))) {
outputOrphan.writeInt(filteredOrphans.size());
Set<Entry<String, int[]>> elements = filteredOrphans.entrySet();
for (Entry<String, int[]> entry : elements) {
outputOrphan.writeUTF(entry.getKey());
saveArray(entry.getValue(), outputOrphan);
}
for (Entry<String, int[]> entry : elements) {
mainOutput.writeInt(entry.getValue().length);
saveExtensions(
(IExtension[]) objectManager.getHandles(entry.getValue(), RegistryObjectManager.EXTENSION),
mainOutput);
}
outputOrphan.flush();
fosOrphan.getFD().sync();
}
outputOrphan.flush();
fosOrphan.getFD().sync();
outputOrphan.close();
}

private void log(Status status) {
Expand Down
Loading

0 comments on commit 8983be4

Please sign in to comment.