Skip to content

Commit a8a1c49

Browse files
EcljpseB0Tjukzi
authored andcommitted
use more File byte[] API
avoid "file.setContents(new ByteArrayInputStream("
1 parent c5603d2 commit a8a1c49

File tree

16 files changed

+64
-54
lines changed

16 files changed

+64
-54
lines changed

resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/FileSystemResourceManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,6 @@ public boolean internalWrite(IProject target, IProjectDescription description, i
704704
if (!descriptionChanged(descriptionFile, newContents))
705705
return false;
706706
}
707-
ByteArrayInputStream in = new ByteArrayInputStream(newContents);
708707
IFileStore descriptionFileStore = ((Resource) descriptionFile).getStore();
709708
IFileInfo fileInfo = descriptionFileStore.fetchInfo();
710709

@@ -717,7 +716,7 @@ public boolean internalWrite(IProject target, IProjectDescription description, i
717716
}
718717

719718
//write the project description file (don't use API because scheduling rule might not match)
720-
write(descriptionFile, in, fileInfo, IResource.FORCE, false, SubMonitor.convert(null));
719+
write(descriptionFile, newContents, fileInfo, IResource.FORCE, false, SubMonitor.convert(null));
721720
workspace.getAliasManager().updateAliases(descriptionFile, getStore(descriptionFile), IResource.DEPTH_ZERO, SubMonitor.convert(null));
722721

723722
//update the timestamp on the project as well so we know when it has

resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ProjectPreferences.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package org.eclipse.core.internal.resources;
1919

2020
import java.io.BufferedInputStream;
21-
import java.io.ByteArrayInputStream;
2221
import java.io.IOException;
2322
import java.io.InputStream;
2423
import java.nio.charset.StandardCharsets;
@@ -707,7 +706,7 @@ protected void save() throws BackingStoreException {
707706
String fileLineSeparator = fileInWorkspace.getLineSeparator(true);
708707
if (!systemLineSeparator.equals(fileLineSeparator))
709708
s = s.replaceAll(systemLineSeparator, fileLineSeparator);
710-
InputStream input = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
709+
byte[] input = s.getBytes(StandardCharsets.UTF_8);
711710
// make sure that preference folder and file are in sync
712711
fileInWorkspace.getParent().refreshLocal(IResource.DEPTH_ZERO, null);
713712
fileInWorkspace.refreshLocal(IResource.DEPTH_ZERO, null);

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/builders/BuildDeltaVerificationTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,7 @@ public void testAddProject() throws CoreException {
216216
public void testChangeFile() throws CoreException {
217217
/* change file1's contents */
218218
verifier.addExpectedChange(file1, project1, IResourceDelta.CHANGED, IResourceDelta.CONTENT);
219-
ByteArrayInputStream in = new ByteArrayInputStream(new byte[] { 4, 5, 6 });
220-
file1.setContents(in, true, false, createTestMonitor());
219+
file1.setContents(new byte[] { 4, 5, 6 }, true, false, createTestMonitor());
221220
rebuild();
222221
assertDelta();
223222
}

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/builders/BuilderTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import static org.junit.Assert.assertThrows;
3232
import static org.junit.Assert.assertTrue;
3333

34-
import java.io.ByteArrayInputStream;
3534
import java.io.ByteArrayOutputStream;
3635
import java.io.IOException;
3736
import java.io.InputStream;
@@ -906,7 +905,7 @@ public void testIncrementalBuildBeforeAutobuild() throws Exception {
906905
final ByteArrayOutputStream out = new ByteArrayOutputStream();
907906
AtomicReference<IOException> exception = new AtomicReference<>();
908907
getWorkspace().run((IWorkspaceRunnable) monitor -> {
909-
input.setContents(new ByteArrayInputStream(new byte[] { 5, 4, 3, 2, 1 }), IResource.NONE, createTestMonitor());
908+
input.setContents(new byte[] { 5, 4, 3, 2, 1 }, IResource.NONE, createTestMonitor());
910909
project.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, createTestMonitor());
911910
try (InputStream inputStream = output.getContents()) {
912911
inputStream.transferTo(out);

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/builders/SortBuilder.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,24 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.tests.internal.builders;
1515

16-
import java.io.*;
16+
import java.io.ByteArrayOutputStream;
17+
import java.io.IOException;
18+
import java.io.InputStream;
1719
import java.util.ArrayList;
1820
import java.util.Map;
1921
import org.eclipse.core.internal.resources.ResourceException;
20-
import org.eclipse.core.resources.*;
21-
import org.eclipse.core.runtime.*;
22+
import org.eclipse.core.resources.IFile;
23+
import org.eclipse.core.resources.IFolder;
24+
import org.eclipse.core.resources.IProject;
25+
import org.eclipse.core.resources.IResource;
26+
import org.eclipse.core.resources.IResourceDelta;
27+
import org.eclipse.core.resources.IResourceStatus;
28+
import org.eclipse.core.resources.IncrementalProjectBuilder;
29+
import org.eclipse.core.runtime.CoreException;
30+
import org.eclipse.core.runtime.IPath;
31+
import org.eclipse.core.runtime.IProgressMonitor;
32+
import org.eclipse.core.runtime.IStatus;
33+
import org.eclipse.core.runtime.Status;
2234

2335
/**
2436
* A <code>SortBuilder</code> maintains a collection of files located
@@ -125,8 +137,7 @@ private void build(IFile unsortedFile) throws CoreException {
125137
IFile sortedFile = (IFile) convertToSortedResource(unsortedFile);
126138
createResource(sortedFile);
127139

128-
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
129-
sortedFile.setContents(bis, true, false, null);
140+
sortedFile.setContents(bytes, true, false, null);
130141
}
131142

132143
@Override

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/Bug544975Test.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static org.junit.Assert.assertFalse;
1919
import static org.junit.Assert.assertTrue;
2020

21-
import java.io.ByteArrayInputStream;
2221
import java.nio.file.Files;
2322
import java.nio.file.Path;
2423
import java.nio.file.Paths;
@@ -116,8 +115,7 @@ public void testBug544975ProjectOpenWithoutBackgroundRefresh() throws Exception
116115
private IFile createFile(IProject project, String fileName, String initialContents) throws CoreException {
117116
IFile file = project.getFile(fileName);
118117
file.create(null, true, createTestMonitor());
119-
ByteArrayInputStream stream = new ByteArrayInputStream(initialContents.getBytes());
120-
file.setContents(stream, IResource.FORCE, new NullProgressMonitor());
118+
file.setContents(initialContents.getBytes(), IResource.FORCE, new NullProgressMonitor());
121119
return file;
122120
}
123121

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/Bug552185PerformanceTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,19 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.tests.internal.resources;
1515

16-
import java.io.ByteArrayInputStream;
1716
import org.eclipse.core.internal.resources.Workspace;
18-
import org.eclipse.core.resources.*;
19-
import org.eclipse.core.runtime.*;
17+
import org.eclipse.core.resources.IFile;
18+
import org.eclipse.core.resources.IFolder;
19+
import org.eclipse.core.resources.IProject;
20+
import org.eclipse.core.resources.IResource;
21+
import org.eclipse.core.resources.IWorkspaceRoot;
22+
import org.eclipse.core.resources.ResourcesPlugin;
23+
import org.eclipse.core.resources.WorkspaceJob;
24+
import org.eclipse.core.runtime.CoreException;
25+
import org.eclipse.core.runtime.IProgressMonitor;
26+
import org.eclipse.core.runtime.IStatus;
27+
import org.eclipse.core.runtime.Status;
28+
import org.eclipse.core.runtime.SubMonitor;
2029

2130
/**
2231
* A benchmark for bug 552185.
@@ -81,8 +90,7 @@ static void runBenchmark(int directoriesCount, int fileCountPerDirectory, IProgr
8190
subMonitor2.checkCanceled();
8291
IFile file = folder.getFile("file" + j);
8392
String content = "file content " + j;
84-
ByteArrayInputStream contentStream = new ByteArrayInputStream(content.getBytes());
85-
file.create(contentStream, IResource.FORCE, subMonitor2);
93+
file.create(content.getBytes(), IResource.FORCE, subMonitor2);
8694
subMonitor2.worked(1);
8795
}
8896
subMonitor.worked(1);

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/ProjectPreferencesTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,7 @@ public void test_95052() throws Exception {
591591
properties.remove("key3");
592592
ByteArrayOutputStream tempOutput = new ByteArrayOutputStream();
593593
properties.store(tempOutput, null);
594-
ByteArrayInputStream tempInput = new ByteArrayInputStream(tempOutput.toByteArray());
595-
prefFile.setContents(tempInput, false, false, createTestMonitor());
594+
prefFile.setContents(tempOutput.toByteArray(), false, false, createTestMonitor());
596595

597596
// here, project preferences should have caught up with the changes
598597
node = new ProjectScope(project).getNode(ResourcesPlugin.PI_RESOURCES);
@@ -642,8 +641,7 @@ public void test_579372() throws Exception {
642641

643642
ByteArrayOutputStream tempOutput = new ByteArrayOutputStream();
644643
properties.store(tempOutput, null);
645-
ByteArrayInputStream tempInput = new ByteArrayInputStream(tempOutput.toByteArray());
646-
prefFile.setContents(tempInput, false, false, createTestMonitor());
644+
prefFile.setContents(tempOutput.toByteArray(), false, false, createTestMonitor());
647645

648646
// here, project preferences should have caught up with the changes
649647
node = new ProjectScope(project).getNode(ResourcesPlugin.PI_RESOURCES);

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/CharsetTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@
3535
import static org.junit.Assert.assertThrows;
3636
import static org.junit.Assert.assertTrue;
3737

38-
import java.io.ByteArrayInputStream;
3938
import java.io.FileWriter;
4039
import java.io.IOException;
41-
import java.io.InputStream;
4240
import java.io.Reader;
4341
import java.io.StringReader;
4442
import java.nio.charset.StandardCharsets;
@@ -480,7 +478,7 @@ public void testBug186984() throws Exception {
480478
// returns the correct value.
481479

482480
// 1) first set the content type to ascii
483-
file.setContents(new ByteArrayInputStream(ascii.getBytes("ascii")), IResource.FORCE, createTestMonitor());
481+
file.setContents(ascii.getBytes("ascii"), IResource.FORCE, createTestMonitor());
484482
assertTrue("4.0", file.getCharset().equals("ascii"));
485483
assertTrue("4.1", file.getContentDescription().getCharset().equals("ascii"));
486484

@@ -904,23 +902,26 @@ public void testContentBasedCharset() throws CoreException {
904902
createInWorkspace(file, SAMPLE_XML_US_ASCII_ENCODING);
905903
assertEquals("1.0", "US-ASCII", file.getCharset());
906904
// content-based encoding is FRED
907-
file.setContents(new ByteArrayInputStream(SAMPLE_XML_ISO_8859_1_ENCODING.getBytes(StandardCharsets.ISO_8859_1)), false, false, null);
905+
file.setContents(SAMPLE_XML_ISO_8859_1_ENCODING.getBytes(StandardCharsets.ISO_8859_1), false, false, null);
908906
assertEquals("2.0", "ISO-8859-1", file.getCharset());
909907
// content-based encoding is UTF-8 (default for XML)
910-
file.setContents(new ByteArrayInputStream(SAMPLE_XML_DEFAULT_ENCODING.getBytes(StandardCharsets.UTF_8)), false, false, null);
908+
file.setContents(SAMPLE_XML_DEFAULT_ENCODING.getBytes(StandardCharsets.UTF_8), false, false, null);
911909
assertEquals("3.0", "UTF-8", file.getCharset());
912910
// tests with BOM -BOMs are strings for convenience, encoded itno bytes using ISO-8859-1 (which handles 128-255 bytes better)
913911
// tests with UTF-8 BOM
914912
String UTF8_BOM = new String(IContentDescription.BOM_UTF_8, StandardCharsets.ISO_8859_1);
915-
file.setContents(new ByteArrayInputStream((UTF8_BOM + SAMPLE_XML_DEFAULT_ENCODING).getBytes(StandardCharsets.ISO_8859_1)), false, false, null);
913+
file.setContents((UTF8_BOM + SAMPLE_XML_DEFAULT_ENCODING).getBytes(StandardCharsets.ISO_8859_1), false,
914+
false, null);
916915
assertEquals("4.0", "UTF-8", file.getCharset());
917916
// tests with UTF-16 Little Endian BOM
918917
String UTF16_LE_BOM = new String(IContentDescription.BOM_UTF_16LE, StandardCharsets.ISO_8859_1);
919-
file.setContents(new ByteArrayInputStream((UTF16_LE_BOM + SAMPLE_XML_DEFAULT_ENCODING).getBytes(StandardCharsets.ISO_8859_1)), false, false, null);
918+
file.setContents((UTF16_LE_BOM + SAMPLE_XML_DEFAULT_ENCODING).getBytes(StandardCharsets.ISO_8859_1), false,
919+
false, null);
920920
assertEquals("5.0", "UTF-16", file.getCharset());
921921
// tests with UTF-16 Big Endian BOM
922922
String UTF16_BE_BOM = new String(IContentDescription.BOM_UTF_16BE, StandardCharsets.ISO_8859_1);
923-
file.setContents(new ByteArrayInputStream((UTF16_BE_BOM + SAMPLE_XML_DEFAULT_ENCODING).getBytes(StandardCharsets.ISO_8859_1)), false, false, null);
923+
file.setContents((UTF16_BE_BOM + SAMPLE_XML_DEFAULT_ENCODING).getBytes(StandardCharsets.ISO_8859_1), false,
924+
false, null);
924925
assertEquals("6.0", "UTF-16", file.getCharset());
925926
} finally {
926927
clearAllEncodings(project);
@@ -1093,8 +1094,7 @@ public void testDeltaOnPreferenceChanges() throws IOException, CoreException {
10931094
backgroundVerifier.reset();
10941095
backgroundVerifier.addExpectedChange(new IResource[] {project, folder1, file1, file2, resourcesPrefs, resourcesPrefs.getParent()}, IResourceDelta.CHANGED, IResourceDelta.ENCODING);
10951096
// cause a resource change event without actually changing contents
1096-
InputStream contents = new ByteArrayInputStream(prefsContent.getBytes());
1097-
resourcesPrefs.setContents(contents, 0, createTestMonitor());
1097+
resourcesPrefs.setContents(prefsContent.getBytes(), 0, createTestMonitor());
10981098
assertTrue("2.1", backgroundVerifier.waitForEvent(10000));
10991099
assertTrue("2.2 " + backgroundVerifier.getMessage(), backgroundVerifier.isDeltaValid());
11001100

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IWorkspaceRootTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,7 @@ private IFile createFile(IContainer parent, int updateFlags, boolean linked) thr
492492
file.setTeamPrivateMember(true);
493493
}
494494
} else {
495-
try (ByteArrayInputStream inputStream = new ByteArrayInputStream("content".getBytes())) {
496-
file.create(inputStream, updateFlags, createTestMonitor());
497-
}
495+
file.create("content".getBytes(), updateFlags, createTestMonitor());
498496
}
499497
return file;
500498
}

0 commit comments

Comments
 (0)