Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.
/ lanai Public archive

Commit 39b1113

Browse files
turbanoffmrserb
authored andcommittedMar 9, 2021
8262161: Refactor manual I/O stream copying in java.desktop to use new convenience APIs
Reviewed-by: serb, prr
1 parent 4e94760 commit 39b1113

File tree

11 files changed

+32
-131
lines changed

11 files changed

+32
-131
lines changed
 

‎src/java.desktop/share/classes/com/sun/media/sound/DLSSoundbank.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -987,11 +987,7 @@ else if (audioformat.getEncoding().equals(Encoding.PCM_FLOAT))
987987
RIFFWriter data_chunk = writer.writeChunk("data");
988988
AudioInputStream stream = AudioSystem.getAudioInputStream(
989989
audioformat, (AudioInputStream)sample.getData());
990-
byte[] buff = new byte[1024];
991-
int ret;
992-
while ((ret = stream.read(buff)) != -1) {
993-
data_chunk.write(buff, 0, ret);
994-
}
990+
stream.transferTo(data_chunk);
995991
} else {
996992
RIFFWriter data_chunk = writer.writeChunk("data");
997993
ModelByteBuffer databuff = sample.getDataBuffer();

‎src/java.desktop/share/classes/com/sun/media/sound/JavaSoundAudioClip.java

+4-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -60,8 +60,6 @@
6060
@SuppressWarnings("deprecation")
6161
public final class JavaSoundAudioClip implements AudioClip, MetaEventListener, LineListener {
6262

63-
private static final int BUFFER_SIZE = 16384; // number of bytes written each time to the source data line
64-
6563
private long lastPlayCall = 0;
6664
private static final int MINIMUM_PLAY_DELAY = 30;
6765

@@ -359,19 +357,9 @@ private void readStream(AudioInputStream as, long byteLen) throws IOException {
359357
private void readStream(AudioInputStream as) throws IOException {
360358

361359
DirectBAOS baos = new DirectBAOS();
362-
byte[] buffer = new byte[16384];
363-
int bytesRead = 0;
364-
int totalBytesRead = 0;
365-
366-
// this loop may throw an IOException
367-
while( true ) {
368-
bytesRead = as.read(buffer, 0, buffer.length);
369-
if (bytesRead <= 0) {
370-
as.close();
371-
break;
372-
}
373-
totalBytesRead += bytesRead;
374-
baos.write(buffer, 0, bytesRead);
360+
int totalBytesRead;
361+
try (as) {
362+
totalBytesRead = (int) as.transferTo(baos);
375363
}
376364
loadedAudio = baos.getInternalBuffer();
377365
loadedAudioByteLength = totalBytesRead;

‎src/java.desktop/share/classes/com/sun/media/sound/ModelByteBuffer.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -201,11 +201,7 @@ public ModelByteBuffer(File file, long offset, long len) {
201201
public void writeTo(OutputStream out) throws IOException {
202202
if (root.file != null && root.buffer == null) {
203203
try (InputStream is = getInputStream()) {
204-
byte[] buff = new byte[1024];
205-
int ret;
206-
while ((ret = is.read(buff)) != -1) {
207-
out.write(buff, 0, ret);
208-
}
204+
is.transferTo(out);
209205
}
210206
} else
211207
out.write(array(), (int) arrayOffset(), (int) capacity());

‎src/java.desktop/share/classes/com/sun/media/sound/StandardMidiFileWriter.java

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -68,7 +68,6 @@ public final class StandardMidiFileWriter extends MidiFileWriter {
6868
private static final int MIDI_TYPE_0 = 0;
6969
private static final int MIDI_TYPE_1 = 1;
7070

71-
private static final int bufferSize = 16384; // buffersize for write
7271
private DataOutputStream tddos; // data output stream for track writing
7372

7473
/**
@@ -117,22 +116,12 @@ public int write(Sequence in, int type, OutputStream out) throws IOException {
117116
if (!isFileTypeSupported(type, in)) {
118117
throw new IllegalArgumentException("Could not write MIDI file");
119118
}
120-
byte [] buffer = null;
121-
122-
int bytesRead = 0;
123-
long bytesWritten = 0;
124-
125119
// First get the fileStream from this sequence
126120
InputStream fileStream = getFileStream(type,in);
127121
if (fileStream == null) {
128122
throw new IllegalArgumentException("Could not write MIDI file");
129123
}
130-
buffer = new byte[bufferSize];
131-
132-
while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
133-
out.write( buffer, 0, bytesRead );
134-
bytesWritten += bytesRead;
135-
}
124+
long bytesWritten = fileStream.transferTo(out);
136125
// Done....return bytesWritten
137126
return (int) bytesWritten;
138127
}

‎src/java.desktop/share/classes/com/sun/media/sound/WaveFloatFileWriter.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -80,11 +80,7 @@ public void write(AudioInputStream stream, RIFFWriter writer)
8080
fmt_chunk.writeUnsignedShort(format.getSampleSizeInBits());
8181
}
8282
try (RIFFWriter data_chunk = writer.writeChunk("data")) {
83-
byte[] buff = new byte[1024];
84-
int len;
85-
while ((len = stream.read(buff, 0, buff.length)) != -1) {
86-
data_chunk.write(buff, 0, len);
87-
}
83+
stream.transferTo(data_chunk);
8884
}
8985
}
9086

‎src/java.desktop/share/classes/javax/swing/plaf/basic/BasicLookAndFeel.java

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -2080,19 +2080,9 @@ public byte[] run() {
20802080
if (resource == null) {
20812081
return null;
20822082
}
2083-
BufferedInputStream in =
2084-
new BufferedInputStream(resource);
2085-
ByteArrayOutputStream out =
2086-
new ByteArrayOutputStream(1024);
2087-
byte[] buffer = new byte[1024];
2088-
int n;
2089-
while ((n = in.read(buffer)) > 0) {
2090-
out.write(buffer, 0, n);
2083+
try (BufferedInputStream in = new BufferedInputStream(resource)) {
2084+
return in.readAllBytes();
20912085
}
2092-
in.close();
2093-
out.flush();
2094-
buffer = out.toByteArray();
2095-
return buffer;
20962086
} catch (IOException ioe) {
20972087
System.err.println(ioe.toString());
20982088
return null;

‎src/java.desktop/share/classes/javax/swing/text/rtf/AbstractFilter.java

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -93,18 +93,7 @@ abstract class AbstractFilter extends OutputStream
9393
public void readFromStream(InputStream in)
9494
throws IOException
9595
{
96-
byte[] buf;
97-
int count;
98-
99-
buf = new byte[16384];
100-
101-
while(true) {
102-
count = in.read(buf);
103-
if (count < 0)
104-
break;
105-
106-
this.write(buf, 0, count);
107-
}
96+
in.transferTo(this);
10897
}
10998

11099
public void readFromReader(Reader in)

‎src/java.desktop/share/classes/sun/awt/datatransfer/DataTransferer.java

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1978,16 +1978,7 @@ public static DataFlavor[] setToSortedDataFlavorArray(Set<DataFlavor> flavorsSet
19781978
protected static byte[] inputStreamToByteArray(InputStream str)
19791979
throws IOException
19801980
{
1981-
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
1982-
int len = 0;
1983-
byte[] buf = new byte[8192];
1984-
1985-
while ((len = str.read(buf)) != -1) {
1986-
baos.write(buf, 0, len);
1987-
}
1988-
1989-
return baos.toByteArray();
1990-
}
1981+
return str.readAllBytes();
19911982
}
19921983

19931984
/**

‎src/java.desktop/share/classes/sun/swing/SwingUtilities2.java

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -58,7 +58,6 @@
5858
import java.awt.print.PrinterGraphics;
5959
import java.beans.PropertyChangeEvent;
6060
import java.io.BufferedInputStream;
61-
import java.io.ByteArrayOutputStream;
6261
import java.io.IOException;
6362
import java.io.InputStream;
6463
import java.lang.reflect.Modifier;
@@ -1754,18 +1753,9 @@ private static byte[] getIconBytes(final Class<?> baseClass,
17541753
continue;
17551754
}
17561755

1757-
try (BufferedInputStream in
1758-
= new BufferedInputStream(resource);
1759-
ByteArrayOutputStream out
1760-
= new ByteArrayOutputStream(1024)) {
1761-
byte[] buffer = new byte[1024];
1762-
int n;
1763-
while ((n = in.read(buffer)) > 0) {
1764-
out.write(buffer, 0, n);
1756+
try (BufferedInputStream in = new BufferedInputStream(resource)) {
1757+
return in.readAllBytes();
17651758
}
1766-
out.flush();
1767-
return out.toByteArray();
1768-
}
17691759
} catch (IOException ioe) {
17701760
System.err.println(ioe.toString());
17711761
}

‎src/java.desktop/unix/classes/sun/print/UnixPrintJob.java

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -596,21 +596,12 @@ public void print(Doc doc, PrintRequestAttributeSet attributes)
596596
}
597597
}
598598
} else if (instream != null) {
599-
BufferedInputStream bin = new BufferedInputStream(instream);
600-
BufferedOutputStream bout = new BufferedOutputStream(output);
601-
byte[] buffer = new byte[1024];
602-
int bread = 0;
603-
604-
try {
605-
while ((bread = bin.read(buffer)) >= 0) {
606-
bout.write(buffer, 0, bread);
607-
}
608-
bin.close();
609-
bout.flush();
610-
bout.close();
599+
try (BufferedInputStream bin = new BufferedInputStream(instream);
600+
BufferedOutputStream bout = new BufferedOutputStream(output)) {
601+
bin.transferTo(bout);
611602
} catch (IOException e) {
612603
notifyEvent(PrintJobEvent.JOB_FAILED);
613-
throw new PrintException (e);
604+
throw new PrintException(e);
614605
}
615606
}
616607
notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);

‎src/java.desktop/windows/classes/sun/print/Win32PrintJob.java

+5-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,30 +27,26 @@
2727

2828
import java.net.URI;
2929
import java.io.BufferedInputStream;
30-
import java.io.BufferedOutputStream;
3130
import java.io.File;
32-
import java.io.FileOutputStream;
33-
import java.io.InputStream;
34-
import java.io.OutputStream;
3531
import java.io.InputStream;
3632
import java.io.IOException;
37-
import java.io.FileNotFoundException;
3833
import java.io.Reader;
3934
import java.net.URL;
35+
import java.nio.file.Files;
36+
import java.nio.file.Path;
37+
import java.nio.file.StandardCopyOption;
4038
import java.util.Vector;
4139

4240
import javax.print.CancelablePrintJob;
4341
import javax.print.Doc;
4442
import javax.print.DocFlavor;
45-
import javax.print.DocPrintJob;
4643
import javax.print.PrintService;
4744
import javax.print.PrintException;
4845
import javax.print.event.PrintJobEvent;
4946
import javax.print.event.PrintJobListener;
5047
import javax.print.event.PrintJobAttributeListener;
5148

5249
import javax.print.attribute.Attribute;
53-
import javax.print.attribute.AttributeSet;
5450
import javax.print.attribute.AttributeSetUtilities;
5551
import javax.print.attribute.DocAttributeSet;
5652
import javax.print.attribute.HashPrintJobAttributeSet;
@@ -437,18 +433,7 @@ public void print(Doc doc, PrintRequestAttributeSet attributes)
437433

438434
if (mDestination != null) { // if destination attribute is set
439435
try {
440-
FileOutputStream fos = new FileOutputStream(mDestination);
441-
byte []buffer = new byte[1024];
442-
int cread;
443-
444-
while ((cread = instream.read(buffer, 0, buffer.length)) >=0) {
445-
fos.write(buffer, 0, cread);
446-
}
447-
fos.flush();
448-
fos.close();
449-
} catch (FileNotFoundException fnfe) {
450-
notifyEvent(PrintJobEvent.JOB_FAILED);
451-
throw new PrintException(fnfe.toString());
436+
Files.copy(instream, Path.of(mDestination), StandardCopyOption.REPLACE_EXISTING);
452437
} catch (IOException ioe) {
453438
notifyEvent(PrintJobEvent.JOB_FAILED);
454439
throw new PrintException(ioe.toString());

0 commit comments

Comments
 (0)
This repository has been archived.