Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8262161: Refactor manual I/O stream copying in java.desktop to use new convenience APIs #1856

Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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
@@ -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);
This conversation was marked as resolved by turbanoff

This comment has been minimized.

@prrace

prrace Feb 25, 2021
Contributor

Are all these audio streams buffered ? transferTo docs don't say anything in terms of guarantees of what API they call. If it is unbuffered and it just calls read(byte) over and over it would be really slow.

This comment has been minimized.

@mrserb

mrserb Feb 26, 2021
Member

It read the data in chunks of "8192" bytes.

} else {
RIFFWriter data_chunk = writer.writeChunk("data");
ModelByteBuffer databuff = sample.getDataBuffer();
@@ -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
@@ -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;

@@ -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;
@@ -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
@@ -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());
@@ -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
@@ -68,7 +68,6 @@
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

/**
@@ -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;
}
@@ -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
@@ -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);
}
}

@@ -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
@@ -2080,19 +2080,9 @@ private void cancelCurrentSound(Clip clip) {
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;
@@ -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
@@ -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)
@@ -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
@@ -1978,16 +1978,7 @@ public void processDataConversionRequests() {
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();
}

/**
@@ -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
@@ -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;
@@ -1754,18 +1753,9 @@ private static Object makeIcon(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());
}
@@ -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
@@ -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);
This conversation was marked as resolved by turbanoff

This comment has been minimized.

This comment has been minimized.

@mrserb

mrserb Feb 26, 2021
Member

The method itself does not close it, but the "try-with-res" around it should.

This comment has been minimized.

@prrace

prrace Feb 27, 2021
Contributor

ah yes

} catch (IOException e) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException (e);
throw new PrintException(e);
}
}
notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
@@ -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
@@ -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;
@@ -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);
This conversation was marked as resolved by turbanoff

This comment has been minimized.

@prsadhuk

prsadhuk Feb 23, 2021
Contributor

Don't we need to close the instream in finally block?

This comment has been minimized.

@turbanoff

turbanoff Feb 23, 2021
Author Member

It will be closed in notifyEvent(PrintJobEvent.JOB_FAILED); -> closeDataStreams in case of IOException.
And there shouldn't be any other kinds of exceptions.

} catch (IOException ioe) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(ioe.toString());