Updating existing JDK code to use InputStream.transferTo() (jdk) (original) (raw)
Patrick Reinhart patrick at reini.net
Fri May 8 23:00:15 UTC 2015
- Previous message: Updating existing JDK code to use InputStream.transferTo()
- Next message: Updating existing JDK code to use InputStream.transferTo() (jdk)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Here the first batch of changes
Cheers
Patrick
Am 07.05.2015 um 20:44 schrieb Pavel Rappo <pavel.rappo at oracle.com>:
Hi Patrick, That's a good idea! I can sponsor them no problem. -Pavel
diff -r 7101bcceb43d make/src/classes/build/tools/module/ModuleArchive.java --- a/make/src/classes/build/tools/module/ModuleArchive.java Thu May 07 10:19:34 2015 -0700 +++ b/make/src/classes/build/tools/module/ModuleArchive.java Sat May 09 00:45:32 2015 +0200 @@ -186,7 +186,7 @@ switch (section) { case CLASSES: if (!filename.startsWith("_the.") && !filename.equals("javac_state")) - writeEntry(in); + in.transferTo(out); break; case LIBS: writeEntry(in, destFile(nativeDir(filename), filename)); @@ -218,13 +218,6 @@ Files.copy(in, dstFile); } - private void writeEntry(InputStream in) throws IOException { - byte[] buf = new byte[8192]; - int n; - while ((n = in.read(buf)) > 0) - out.write(buf, 0, n); - }
private static String nativeDir(String filename) {
if (System.getProperty("os.name").startsWith("Windows")) {
if (filename.endsWith(".dll") || filename.endsWith(".diz")
diff -r 7101bcceb43d src/java.base/share/classes/java/nio/file/Files.java --- a/src/java.base/share/classes/java/nio/file/Files.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.base/share/classes/java/nio/file/Files.java Sat May 09 00:45:32 2015 +0200 @@ -2904,22 +2904,6 @@ } /** - * Reads all bytes from an input stream and writes them to an output stream. - */ - private static long copy(InputStream source, OutputStream sink) - throws IOException - { - long nread = 0L; - byte[] buf = new byte[BUFFER_SIZE]; - int n; - while ((n = source.read(buf)) > 0) { - sink.write(buf, 0, n); - nread += n; - } - return nread; - }
- /**
Copies all bytes from an input stream to a file. On return, the input
stream will be at end of stream.
@@ -3031,7 +3015,7 @@
// do the copy try (OutputStream out = ostream) {
return copy(in, out);
}return in.transferTo(out); }
@@ -3073,7 +3057,7 @@ Objects.requireNonNull(out); try (InputStream in = newInputStream(source)) { - return copy(in, out); + return in.transferTo(out); } } diff -r 7101bcceb43d src/java.base/share/classes/java/util/jar/JarInputStream.java --- a/src/java.base/share/classes/java/util/jar/JarInputStream.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.base/share/classes/java/util/jar/JarInputStream.java Sat May 09 00:45:32 2015 +0200 @@ -106,12 +106,8 @@ private byte[] getBytes(InputStream is) throws IOException { - byte[] buffer = new byte[8192]; ByteArrayOutputStream baos = new ByteArrayOutputStream(2048); - int n; - while ((n = is.read(buffer, 0, buffer.length)) != -1) { - baos.write(buffer, 0, n); - } + is.transferTo(baos); return baos.toByteArray(); } diff -r 7101bcceb43d src/java.base/share/classes/jdk/internal/jrtfs/JrtPath.java --- a/src/java.base/share/classes/jdk/internal/jrtfs/JrtPath.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.base/share/classes/jdk/internal/jrtfs/JrtPath.java Sat May 09 00:45:32 2015 +0200 @@ -829,11 +829,7 @@ target.createDirectory(); } else { try (InputStream is = jrtfs.newInputStream(getResolvedPath()); OutputStream os = target.newOutputStream()) { - byte[] buf = new byte[8192]; - int n; - while ((n = is.read(buf)) != -1) { - os.write(buf, 0, n); - } + is.transferTo(os); } } if (copyAttrs) { diff -r 7101bcceb43d src/java.base/share/classes/sun/net/www/MimeLauncher.java --- a/src/java.base/share/classes/sun/net/www/MimeLauncher.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.base/share/classes/sun/net/www/MimeLauncher.java Sat May 09 00:45:32 2015 +0200 @@ -108,30 +108,20 @@ public void run() { try { + if (is == null) { + return; + } + String ofn = m.getTempFileTemplate(); if (ofn == null) { ofn = genericTempFileTemplate; } ofn = getTempFileName(uc.getURL(), ofn); - try { - OutputStream os = new FileOutputStream(ofn); - byte buf[] = new byte[2048]; - int i = 0; - try { - while ((i = is.read(buf)) >= 0) { - os.write(buf, 0, i); - } - } catch(IOException e) { - //System.err.println("Exception in write loop " + i); - //e.printStackTrace(); - } finally { - os.close(); - is.close(); - } - } catch(IOException e) { - //System.err.println("Exception in input or output stream"); - //e.printStackTrace(); + try (OutputStream os = new FileOutputStream(ofn)) { + is.transferTo(os); + } finally { + is.close(); } int inx = 0; diff -r 7101bcceb43d src/java.base/share/classes/sun/security/provider/certpath/X509CertPath.java --- a/src/java.base/share/classes/sun/security/provider/certpath/X509CertPath.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.base/share/classes/sun/security/provider/certpath/X509CertPath.java Sat May 09 00:45:32 2015 +0200 @@ -256,12 +256,8 @@ * @return the bytes read from the InputStream */ private static byte[] readAllBytes(InputStream is) throws IOException { - byte[] buffer = new byte[8192]; - ByteArrayOutputStream baos = new ByteArrayOutputStream(2048); - int n; - while ((n = is.read(buffer)) != -1) { - baos.write(buffer, 0, n); - } + ByteArrayOutputStream baos = new ByteArrayOutputStream(8192); + is.transferTo(baos); return baos.toByteArray(); } diff -r 7101bcceb43d src/java.base/share/classes/sun/security/tools/keytool/Main.java --- a/src/java.base/share/classes/sun/security/tools/keytool/Main.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.base/share/classes/sun/security/tools/keytool/Main.java Sat May 09 00:45:32 2015 +0200 @@ -2194,12 +2194,7 @@ // might not work properly, since -gencrl is slow // and there's no data in the pipe at the beginning. ByteArrayOutputStream bout = new ByteArrayOutputStream(); - byte[] b = new byte[4096]; - while (true) { - int len = in.read(b); - if (len < 0) break; - bout.write(b, 0, len); - } + in.transferTo(bout); return CertificateFactory.getInstance("X509").generateCRLs( new ByteArrayInputStream(bout.toByteArray())); } finally { diff -r 7101bcceb43d src/java.desktop/share/classes/com/sun/media/sound/DLSSoundbank.java --- a/src/java.desktop/share/classes/com/sun/media/sound/DLSSoundbank.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.desktop/share/classes/com/sun/media/sound/DLSSoundbank.java Sat May 09 00:45:32 2015 +0200 @@ -984,11 +984,7 @@ RIFFWriter data_chunk = writer.writeChunk("data"); AudioInputStream stream = AudioSystem.getAudioInputStream( audioformat, (AudioInputStream)sample.getData()); - byte[] buff = new byte[1024]; - int ret; - while ((ret = stream.read(buff)) != -1) { - data_chunk.write(buff, 0, ret); - } + stream.transferTo(data_chunk); } else { RIFFWriter data_chunk = writer.writeChunk("data"); ModelByteBuffer databuff = sample.getDataBuffer(); diff -r 7101bcceb43d src/java.desktop/share/classes/com/sun/media/sound/ModelByteBuffer.java --- a/src/java.desktop/share/classes/com/sun/media/sound/ModelByteBuffer.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.desktop/share/classes/com/sun/media/sound/ModelByteBuffer.java Sat May 09 00:45:32 2015 +0200 @@ -190,11 +190,7 @@ public void writeTo(OutputStream out) throws IOException { if (root.file != null && root.buffer == null) { - InputStream is = getInputStream(); - byte[] buff = new byte[1024]; - int ret; - while ((ret = is.read(buff)) != -1) - out.write(buff, 0, ret); + getInputStream().transferTo(out); } else out.write(array(), (int) arrayOffset(), (int) capacity()); } diff -r 7101bcceb43d src/java.desktop/share/classes/com/sun/media/sound/StandardMidiFileWriter.java --- a/src/java.desktop/share/classes/com/sun/media/sound/StandardMidiFileWriter.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.desktop/share/classes/com/sun/media/sound/StandardMidiFileWriter.java Sat May 09 00:45:32 2015 +0200 @@ -125,11 +125,6 @@ } public int write(Sequence in, int type, OutputStream out) throws IOException { - byte [] buffer = null;
int bytesRead = 0;
long bytesWritten = 0;
if( !isFileTypeSupported(type,in) ) { throw new IllegalArgumentException("Could not write MIDI file"); }
@@ -138,14 +133,8 @@ if (fileStream == null) { throw new IllegalArgumentException("Could not write MIDI file"); } - buffer = new byte[bufferSize];
while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
out.write( buffer, 0, bytesRead );
bytesWritten += bytesRead;
} // Done....return bytesWritten
return (int) bytesWritten;
return (int) fileStream.transferTo(out);
}
public int write(Sequence in, int type, File out) throws IOException {
diff -r 7101bcceb43d src/java.desktop/share/classes/com/sun/media/sound/WaveFloatFileWriter.java --- a/src/java.desktop/share/classes/com/sun/media/sound/WaveFloatFileWriter.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.desktop/share/classes/com/sun/media/sound/WaveFloatFileWriter.java Sat May 09 00:45:32 2015 +0200 @@ -66,23 +66,20 @@ public void write(AudioInputStream stream, RIFFWriter writer) throws IOException { - RIFFWriter fmt_chunk = writer.writeChunk("fmt "); + try (RIFFWriter fmt_chunk = writer.writeChunk("fmt ")) { + AudioFormat format = stream.getFormat(); + fmt_chunk.writeUnsignedShort(3); // WAVE_FORMAT_IEEE_FLOAT + fmt_chunk.writeUnsignedShort(format.getChannels()); + fmt_chunk.writeUnsignedInt((int) format.getSampleRate()); + fmt_chunk.writeUnsignedInt(((int) format.getFrameRate()) + * format.getFrameSize()); + fmt_chunk.writeUnsignedShort(format.getFrameSize()); + fmt_chunk.writeUnsignedShort(format.getSampleSizeInBits()); + } - AudioFormat format = stream.getFormat(); - fmt_chunk.writeUnsignedShort(3); // WAVE_FORMAT_IEEE_FLOAT - fmt_chunk.writeUnsignedShort(format.getChannels()); - fmt_chunk.writeUnsignedInt((int) format.getSampleRate()); - fmt_chunk.writeUnsignedInt(((int) format.getFrameRate()) - * format.getFrameSize()); - fmt_chunk.writeUnsignedShort(format.getFrameSize()); - fmt_chunk.writeUnsignedShort(format.getSampleSizeInBits()); - fmt_chunk.close(); - RIFFWriter data_chunk = writer.writeChunk("data"); - byte[] buff = new byte[1024]; - int len; - while ((len = stream.read(buff, 0, buff.length)) != -1) - data_chunk.write(buff, 0, len); - data_chunk.close(); + try (RIFFWriter data_chunk = writer.writeChunk("data")) { + stream.transferTo(data_chunk); + } } private static class NoCloseOutputStream extends OutputStream { diff -r 7101bcceb43d src/java.desktop/share/classes/javax/swing/plaf/basic/BasicLookAndFeel.java --- a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicLookAndFeel.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicLookAndFeel.java Sat May 09 00:45:32 2015 +0200 @@ -2073,19 +2073,13 @@ if (resource == null) { return null; } - BufferedInputStream in = - new BufferedInputStream(resource); ByteArrayOutputStream out = new ByteArrayOutputStream(1024); - byte[] buffer = new byte[1024]; - int n; - while ((n = in.read(buffer)) > 0) { - out.write(buffer, 0, n); + try (BufferedInputStream in = + new BufferedInputStream(resource)) { + in.transferTo(out); } - in.close(); - out.flush(); - buffer = out.toByteArray(); - return buffer; + return out.toByteArray(); } catch (IOException ioe) { System.err.println(ioe.toString()); return null; diff -r 7101bcceb43d src/java.desktop/share/classes/javax/swing/text/rtf/AbstractFilter.java --- a/src/java.desktop/share/classes/javax/swing/text/rtf/AbstractFilter.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.desktop/share/classes/javax/swing/text/rtf/AbstractFilter.java Sat May 09 00:45:32 2015 +0200 @@ -93,18 +93,7 @@ public void readFromStream(InputStream in) throws IOException { - byte buf[]; - int count;
buf = new byte[16384];
while(true) {
count = in.read(buf);
if (count < 0)
break;
this.write(buf, 0, count);
}
in.transferTo(this);
}
public void readFromReader(Reader in)
diff -r 7101bcceb43d src/java.desktop/share/classes/sun/swing/SwingUtilities2.java --- a/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java Sat May 09 00:45:32 2015 +0200 @@ -1568,11 +1568,7 @@ = new BufferedInputStream(resource); ByteArrayOutputStream out = new ByteArrayOutputStream(1024)) { - byte[] buffer = new byte[1024]; - int n; - while ((n = in.read(buffer)) > 0) { - out.write(buffer, 0, n); - } + in.transferTo(out); out.flush(); return out.toByteArray(); } diff -r 7101bcceb43d src/java.desktop/unix/classes/sun/print/UnixPrintJob.java --- a/src/java.desktop/unix/classes/sun/print/UnixPrintJob.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.desktop/unix/classes/sun/print/UnixPrintJob.java Sat May 09 00:45:32 2015 +0200 @@ -591,18 +591,10 @@ } } } else if (instream != null) { - BufferedInputStream bin = new BufferedInputStream(instream); - BufferedOutputStream bout = new BufferedOutputStream(output); - byte[] buffer = new byte[1024]; - int bread = 0;
try {
while ((bread = bin.read(buffer)) >= 0) {
bout.write(buffer, 0, bread);
}
bin.close();
try (BufferedInputStream bin = new BufferedInputStream(instream);
BufferedOutputStream bout = new BufferedOutputStream(output)) {
bin.transferTo(bout); bout.flush();
bout.close(); } catch (IOException e) { notifyEvent(PrintJobEvent.JOB_FAILED); throw new PrintException (e);
diff -r 7101bcceb43d src/java.desktop/windows/classes/sun/print/Win32PrintJob.java --- a/src/java.desktop/windows/classes/sun/print/Win32PrintJob.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.desktop/windows/classes/sun/print/Win32PrintJob.java Sat May 09 00:45:32 2015 +0200 @@ -436,16 +436,9 @@ } if (mDestination != null) { // if destination attribute is set - try { - FileOutputStream fos = new FileOutputStream(mDestination); - byte []buffer = new byte[1024]; - int cread;
while ((cread = instream.read(buffer, 0, buffer.length)) >=0) {
fos.write(buffer, 0, cread);
}
try (FileOutputStream fos = new FileOutputStream(mDestination)) {
instream.transferTo(fos); fos.flush();
fos.close(); } catch (FileNotFoundException fnfe) { notifyEvent(PrintJobEvent.JOB_FAILED); throw new PrintException(fnfe.toString());
diff -r 7101bcceb43d src/java.management/share/classes/javax/management/loading/MLet.java --- a/src/java.management/share/classes/javax/management/loading/MLet.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.management/share/classes/javax/management/loading/MLet.java Sat May 09 00:45:32 2015 +0200 @@ -1158,28 +1158,19 @@ InputStream is = getResourceAsStream( libname.replace(File.separatorChar,'/')); if (is != null) { - try { + try (is) { File directory = new File(libraryDirectory); directory.mkdirs(); File file = Files.createTempFile(directory.toPath(), libname + ".", null) .toFile(); file.deleteOnExit(); - FileOutputStream fileOutput = new FileOutputStream(file); - try { - byte[] buf = new byte[4096]; - int n; - while ((n = is.read(buf)) >= 0) { - fileOutput.write(buf, 0, n); - } - } finally { - fileOutput.close(); + try (FileOutputStream fileOutput = new FileOutputStream(file)) { + is.transferTo(fileOutput); } if (file.exists()) { return file.getAbsolutePath(); } - } finally { - is.close(); } } } catch (Exception e) { diff -r 7101bcceb43d src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/utils/JavaUtils.java --- a/src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/utils/JavaUtils.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/utils/JavaUtils.java Sat May 09 00:45:32 2015 +0200 @@ -59,32 +59,11 @@ */ public static byte[] getBytesFromFile(String fileName) throws FileNotFoundException, IOException {
byte refBytes[] = null;
FileInputStream fisRef = null;
UnsyncByteArrayOutputStream baos = null;
try {
fisRef = new FileInputStream(fileName);
baos = new UnsyncByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
while ((len = fisRef.read(buf)) > 0) {
baos.write(buf, 0, len);
}
refBytes = baos.toByteArray();
} finally {
if (baos != null) {
baos.close();
}
if (fisRef != null) {
fisRef.close();
}
try (FileInputStream fisRef = new FileInputStream(fileName);
UnsyncByteArrayOutputStream baos = new UnsyncByteArrayOutputStream()) {
fisRef.transferTo(baos);
return baos.toByteArray(); }
return refBytes;
}
/**
@@ -94,30 +73,17 @@ * @param bytes */ public static void writeBytesToFilename(String filename, byte[] bytes) { - FileOutputStream fos = null; - try { - if (filename != null && bytes != null) { - File f = new File(filename);
fos = new FileOutputStream(f);
if (filename != null && bytes != null) {
File f = new File(filename);
try (FileOutputStream fos = new FileOutputStream(f)) { fos.write(bytes);
fos.close();
} else {
} catch (IOException ex) { if (log.isLoggable(java.util.logging.Level.FINE)) {
log.log(java.util.logging.Level.FINE, "writeBytesToFilename got null byte[] pointed");
log.log(java.util.logging.Level.FINE, ex.getMessage(), ex); } }
} catch (IOException ex) {
if (fos != null) {
try {
fos.close();
} catch (IOException ioe) {
if (log.isLoggable(java.util.logging.Level.FINE)) {
log.log(java.util.logging.Level.FINE, ioe.getMessage(), ioe);
}
}
}
} else if (log.isLoggable(java.util.logging.Level.FINE)) {
}log.log(java.util.logging.Level.FINE, "writeBytesToFilename got null byte[] pointed"); }
@@ -132,22 +98,11 @@ * @throws IOException */ public static byte[] getBytesFromStream(InputStream inputStream) throws IOException { - UnsyncByteArrayOutputStream baos = null;
byte[] retBytes = null;
try {
baos = new UnsyncByteArrayOutputStream();
byte buf[] = new byte[4 * 1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
baos.write(buf, 0, len);
}
try (UnsyncByteArrayOutputStream baos = new UnsyncByteArrayOutputStream()) {
inputStream.transferTo(baos); retBytes = baos.toByteArray();
} finally {
baos.close(); }
return retBytes; }
diff -r 7101bcceb43d src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/utils/resolver/implementations/ResolverDirectHTTP.java --- a/src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/utils/resolver/implementations/ResolverDirectHTTP.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/utils/resolver/implementations/ResolverDirectHTTP.java Sat May 09 00:45:32 2015 +0200 @@ -142,14 +142,7 @@ String mimeType = urlConnection.getHeaderField("Content-Type"); inputStream = urlConnection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); - byte buf[] = new byte[4096]; - int read = 0; - int summarized = 0;
while ((read = inputStream.read(buf)) >= 0) {
baos.write(buf, 0, read);
summarized += read;
}
long summarized = inputStream.transferTo(baos); if (log.isLoggable(java.util.logging.Level.FINE)) { log.log(java.util.logging.Level.FINE, "Fetched " + summarized + " bytes from URI " + uriNew.toString());
diff -r 7101bcceb43d src/java.xml.crypto/share/classes/org/jcp/xml/dsig/internal/dom/Utils.java --- a/src/java.xml.crypto/share/classes/org/jcp/xml/dsig/internal/dom/Utils.java Thu May 07 10:19:34 2015 -0700 +++ b/src/java.xml.crypto/share/classes/org/jcp/xml/dsig/internal/dom/Utils.java Sat May 09 00:45:32 2015 +0200 @@ -49,17 +49,7 @@ throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
while (true) {
int read = is.read(buf);
if (read == -1) { // EOF
break;
}
baos.write(buf, 0, read);
if (read < 1024) {
break;
}
}
}is.transferTo(baos); return baos.toByteArray();
diff -r 7101bcceb43d src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipPath.java --- a/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipPath.java Thu May 07 10:19:34 2015 -0700 +++ b/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipPath.java Sat May 09 00:45:32 2015 +0200 @@ -834,20 +834,9 @@ // create directory or file target.createDirectory(); } else {
InputStream is = zfs.newInputStream(getResolvedPath());
try {
OutputStream os = target.newOutputStream();
try {
byte[] buf = new byte[8192];
int n = 0;
while ((n = is.read(buf)) != -1) {
os.write(buf, 0, n);
}
} finally {
os.close();
}
} finally {
is.close();
try (InputStream is = zfs.newInputStream(getResolvedPath());
OutputStream os = target.newOutputStream()) {
is.transferTo(os); } } if (copyAttrs) {
- Previous message: Updating existing JDK code to use InputStream.transferTo()
- Next message: Updating existing JDK code to use InputStream.transferTo() (jdk)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]