Skip to content

Commit

Permalink
8262161: Refactor manual I/O stream copying in java.desktop to use ne…
Browse files Browse the repository at this point in the history
…w convenience APIs

Reviewed-by: serb, prr
  • Loading branch information
turbanoff authored and mrserb committed Mar 9, 2021
1 parent 4e94760 commit 39b1113
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 131 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -987,11 +987,7 @@ else if (audioformat.getEncoding().equals(Encoding.PCM_FLOAT))
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();
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -60,8 +60,6 @@
@SuppressWarnings("deprecation")
public final class JavaSoundAudioClip implements AudioClip, MetaEventListener, LineListener {

private static final int BUFFER_SIZE = 16384; // number of bytes written each time to the source data line

private long lastPlayCall = 0;
private static final int MINIMUM_PLAY_DELAY = 30;

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

DirectBAOS baos = new DirectBAOS();
byte[] buffer = new byte[16384];
int bytesRead = 0;
int totalBytesRead = 0;

// this loop may throw an IOException
while( true ) {
bytesRead = as.read(buffer, 0, buffer.length);
if (bytesRead <= 0) {
as.close();
break;
}
totalBytesRead += bytesRead;
baos.write(buffer, 0, bytesRead);
int totalBytesRead;
try (as) {
totalBytesRead = (int) as.transferTo(baos);
}
loadedAudio = baos.getInternalBuffer();
loadedAudioByteLength = totalBytesRead;
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -201,11 +201,7 @@ public ModelByteBuffer(File file, long offset, long len) {
public void writeTo(OutputStream out) throws IOException {
if (root.file != null && root.buffer == null) {
try (InputStream is = getInputStream()) {
byte[] buff = new byte[1024];
int ret;
while ((ret = is.read(buff)) != -1) {
out.write(buff, 0, ret);
}
is.transferTo(out);
}
} else
out.write(array(), (int) arrayOffset(), (int) capacity());
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -68,7 +68,6 @@ public final class StandardMidiFileWriter extends MidiFileWriter {
private static final int MIDI_TYPE_0 = 0;
private static final int MIDI_TYPE_1 = 1;

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

/**
Expand Down Expand Up @@ -117,22 +116,12 @@ public int write(Sequence in, int type, OutputStream out) throws IOException {
if (!isFileTypeSupported(type, in)) {
throw new IllegalArgumentException("Could not write MIDI file");
}
byte [] buffer = null;

int bytesRead = 0;
long bytesWritten = 0;

// First get the fileStream from this sequence
InputStream fileStream = getFileStream(type,in);
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;
}
long bytesWritten = fileStream.transferTo(out);
// Done....return bytesWritten
return (int) bytesWritten;
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -80,11 +80,7 @@ public void write(AudioInputStream stream, RIFFWriter writer)
fmt_chunk.writeUnsignedShort(format.getSampleSizeInBits());
}
try (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);
}
stream.transferTo(data_chunk);
}
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -2080,19 +2080,9 @@ public byte[] run() {
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)) {
return in.readAllBytes();
}
in.close();
out.flush();
buffer = out.toByteArray();
return buffer;
} catch (IOException ioe) {
System.err.println(ioe.toString());
return null;
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -93,18 +93,7 @@ abstract class AbstractFilter extends OutputStream
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)
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -1978,16 +1978,7 @@ public static DataFlavor[] setToSortedDataFlavorArray(Set<DataFlavor> flavorsSet
protected static byte[] inputStreamToByteArray(InputStream str)
throws IOException
{
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
int len = 0;
byte[] buf = new byte[8192];

while ((len = str.read(buf)) != -1) {
baos.write(buf, 0, len);
}

return baos.toByteArray();
}
return str.readAllBytes();
}

/**
Expand Down
16 changes: 3 additions & 13 deletions src/java.desktop/share/classes/sun/swing/SwingUtilities2.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -58,7 +58,6 @@
import java.awt.print.PrinterGraphics;
import java.beans.PropertyChangeEvent;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -1754,18 +1753,9 @@ private static byte[] getIconBytes(final Class<?> baseClass,
continue;
}

try (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)) {
return in.readAllBytes();
}
out.flush();
return out.toByteArray();
}
} catch (IOException ioe) {
System.err.println(ioe.toString());
}
Expand Down
19 changes: 5 additions & 14 deletions src/java.desktop/unix/classes/sun/print/UnixPrintJob.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -596,21 +596,12 @@ public void print(Doc doc, PrintRequestAttributeSet attributes)
}
}
} 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();
bout.flush();
bout.close();
try (BufferedInputStream bin = new BufferedInputStream(instream);
BufferedOutputStream bout = new BufferedOutputStream(output)) {
bin.transferTo(bout);
} catch (IOException e) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException (e);
throw new PrintException(e);
}
}
notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
Expand Down
25 changes: 5 additions & 20 deletions src/java.desktop/windows/classes/sun/print/Win32PrintJob.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -27,30 +27,26 @@

import java.net.URI;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.Reader;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Vector;

import javax.print.CancelablePrintJob;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintException;
import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;
import javax.print.event.PrintJobAttributeListener;

import javax.print.attribute.Attribute;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.AttributeSetUtilities;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashPrintJobAttributeSet;
Expand Down Expand Up @@ -437,18 +433,7 @@ public void print(Doc doc, PrintRequestAttributeSet attributes)

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);
}
fos.flush();
fos.close();
} catch (FileNotFoundException fnfe) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(fnfe.toString());
Files.copy(instream, Path.of(mDestination), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ioe) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(ioe.toString());
Expand Down

1 comment on commit 39b1113

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.