8262161: Refactor manual I/O stream copying in java.desktop to use new convenience APIs
Reviewed-by: serb, prr
This commit is contained in:
parent
4e9476071d
commit
39b1113838
@ -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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -987,11 +987,7 @@ public final class DLSSoundbank implements Soundbank {
|
|||||||
RIFFWriter data_chunk = writer.writeChunk("data");
|
RIFFWriter data_chunk = writer.writeChunk("data");
|
||||||
AudioInputStream stream = AudioSystem.getAudioInputStream(
|
AudioInputStream stream = AudioSystem.getAudioInputStream(
|
||||||
audioformat, (AudioInputStream)sample.getData());
|
audioformat, (AudioInputStream)sample.getData());
|
||||||
byte[] buff = new byte[1024];
|
stream.transferTo(data_chunk);
|
||||||
int ret;
|
|
||||||
while ((ret = stream.read(buff)) != -1) {
|
|
||||||
data_chunk.write(buff, 0, ret);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
RIFFWriter data_chunk = writer.writeChunk("data");
|
RIFFWriter data_chunk = writer.writeChunk("data");
|
||||||
ModelByteBuffer databuff = sample.getDataBuffer();
|
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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -60,8 +60,6 @@ import javax.sound.sampled.UnsupportedAudioFileException;
|
|||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
public final class JavaSoundAudioClip implements AudioClip, MetaEventListener, LineListener {
|
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 long lastPlayCall = 0;
|
||||||
private static final int MINIMUM_PLAY_DELAY = 30;
|
private static final int MINIMUM_PLAY_DELAY = 30;
|
||||||
|
|
||||||
@ -359,19 +357,9 @@ public final class JavaSoundAudioClip implements AudioClip, MetaEventListener, L
|
|||||||
private void readStream(AudioInputStream as) throws IOException {
|
private void readStream(AudioInputStream as) throws IOException {
|
||||||
|
|
||||||
DirectBAOS baos = new DirectBAOS();
|
DirectBAOS baos = new DirectBAOS();
|
||||||
byte[] buffer = new byte[16384];
|
int totalBytesRead;
|
||||||
int bytesRead = 0;
|
try (as) {
|
||||||
int totalBytesRead = 0;
|
totalBytesRead = (int) as.transferTo(baos);
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
loadedAudio = baos.getInternalBuffer();
|
loadedAudio = baos.getInternalBuffer();
|
||||||
loadedAudioByteLength = totalBytesRead;
|
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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -201,11 +201,7 @@ public final class ModelByteBuffer {
|
|||||||
public void writeTo(OutputStream out) throws IOException {
|
public void writeTo(OutputStream out) throws IOException {
|
||||||
if (root.file != null && root.buffer == null) {
|
if (root.file != null && root.buffer == null) {
|
||||||
try (InputStream is = getInputStream()) {
|
try (InputStream is = getInputStream()) {
|
||||||
byte[] buff = new byte[1024];
|
is.transferTo(out);
|
||||||
int ret;
|
|
||||||
while ((ret = is.read(buff)) != -1) {
|
|
||||||
out.write(buff, 0, ret);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
out.write(array(), (int) arrayOffset(), (int) capacity());
|
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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -68,7 +68,6 @@ public final class StandardMidiFileWriter extends MidiFileWriter {
|
|||||||
private static final int MIDI_TYPE_0 = 0;
|
private static final int MIDI_TYPE_0 = 0;
|
||||||
private static final int MIDI_TYPE_1 = 1;
|
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
|
private DataOutputStream tddos; // data output stream for track writing
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -117,22 +116,12 @@ public final class StandardMidiFileWriter extends MidiFileWriter {
|
|||||||
if (!isFileTypeSupported(type, in)) {
|
if (!isFileTypeSupported(type, in)) {
|
||||||
throw new IllegalArgumentException("Could not write MIDI file");
|
throw new IllegalArgumentException("Could not write MIDI file");
|
||||||
}
|
}
|
||||||
byte [] buffer = null;
|
|
||||||
|
|
||||||
int bytesRead = 0;
|
|
||||||
long bytesWritten = 0;
|
|
||||||
|
|
||||||
// First get the fileStream from this sequence
|
// First get the fileStream from this sequence
|
||||||
InputStream fileStream = getFileStream(type,in);
|
InputStream fileStream = getFileStream(type,in);
|
||||||
if (fileStream == null) {
|
if (fileStream == null) {
|
||||||
throw new IllegalArgumentException("Could not write MIDI file");
|
throw new IllegalArgumentException("Could not write MIDI file");
|
||||||
}
|
}
|
||||||
buffer = new byte[bufferSize];
|
long bytesWritten = fileStream.transferTo(out);
|
||||||
|
|
||||||
while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
|
|
||||||
out.write( buffer, 0, bytesRead );
|
|
||||||
bytesWritten += bytesRead;
|
|
||||||
}
|
|
||||||
// Done....return bytesWritten
|
// Done....return bytesWritten
|
||||||
return (int) 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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -80,11 +80,7 @@ public final class WaveFloatFileWriter extends AudioFileWriter {
|
|||||||
fmt_chunk.writeUnsignedShort(format.getSampleSizeInBits());
|
fmt_chunk.writeUnsignedShort(format.getSampleSizeInBits());
|
||||||
}
|
}
|
||||||
try (RIFFWriter data_chunk = writer.writeChunk("data")) {
|
try (RIFFWriter data_chunk = writer.writeChunk("data")) {
|
||||||
byte[] buff = new byte[1024];
|
stream.transferTo(data_chunk);
|
||||||
int len;
|
|
||||||
while ((len = stream.read(buff, 0, buff.length)) != -1) {
|
|
||||||
data_chunk.write(buff, 0, len);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -2080,19 +2080,9 @@ public abstract class BasicLookAndFeel extends LookAndFeel implements Serializab
|
|||||||
if (resource == null) {
|
if (resource == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
BufferedInputStream in =
|
try (BufferedInputStream in = new BufferedInputStream(resource)) {
|
||||||
new BufferedInputStream(resource);
|
return in.readAllBytes();
|
||||||
ByteArrayOutputStream out =
|
|
||||||
new ByteArrayOutputStream(1024);
|
|
||||||
byte[] buffer = new byte[1024];
|
|
||||||
int n;
|
|
||||||
while ((n = in.read(buffer)) > 0) {
|
|
||||||
out.write(buffer, 0, n);
|
|
||||||
}
|
}
|
||||||
in.close();
|
|
||||||
out.flush();
|
|
||||||
buffer = out.toByteArray();
|
|
||||||
return buffer;
|
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
System.err.println(ioe.toString());
|
System.err.println(ioe.toString());
|
||||||
return null;
|
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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -93,18 +93,7 @@ abstract class AbstractFilter extends OutputStream
|
|||||||
public void readFromStream(InputStream in)
|
public void readFromStream(InputStream in)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
byte[] buf;
|
in.transferTo(this);
|
||||||
int count;
|
|
||||||
|
|
||||||
buf = new byte[16384];
|
|
||||||
|
|
||||||
while(true) {
|
|
||||||
count = in.read(buf);
|
|
||||||
if (count < 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
this.write(buf, 0, count);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void readFromReader(Reader in)
|
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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -1978,16 +1978,7 @@ search:
|
|||||||
protected static byte[] inputStreamToByteArray(InputStream str)
|
protected static byte[] inputStreamToByteArray(InputStream str)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
return str.readAllBytes();
|
||||||
int len = 0;
|
|
||||||
byte[] buf = new byte[8192];
|
|
||||||
|
|
||||||
while ((len = str.read(buf)) != -1) {
|
|
||||||
baos.write(buf, 0, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
return baos.toByteArray();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -58,7 +58,6 @@ import java.awt.geom.Rectangle2D;
|
|||||||
import java.awt.print.PrinterGraphics;
|
import java.awt.print.PrinterGraphics;
|
||||||
import java.beans.PropertyChangeEvent;
|
import java.beans.PropertyChangeEvent;
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
@ -1754,18 +1753,9 @@ public class SwingUtilities2 {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
try (BufferedInputStream in
|
try (BufferedInputStream in = new BufferedInputStream(resource)) {
|
||||||
= new BufferedInputStream(resource);
|
return in.readAllBytes();
|
||||||
ByteArrayOutputStream out
|
|
||||||
= new ByteArrayOutputStream(1024)) {
|
|
||||||
byte[] buffer = new byte[1024];
|
|
||||||
int n;
|
|
||||||
while ((n = in.read(buffer)) > 0) {
|
|
||||||
out.write(buffer, 0, n);
|
|
||||||
}
|
}
|
||||||
out.flush();
|
|
||||||
return out.toByteArray();
|
|
||||||
}
|
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
System.err.println(ioe.toString());
|
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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -596,21 +596,12 @@ public class UnixPrintJob implements CancelablePrintJob {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (instream != null) {
|
} else if (instream != null) {
|
||||||
BufferedInputStream bin = new BufferedInputStream(instream);
|
try (BufferedInputStream bin = new BufferedInputStream(instream);
|
||||||
BufferedOutputStream bout = new BufferedOutputStream(output);
|
BufferedOutputStream bout = new BufferedOutputStream(output)) {
|
||||||
byte[] buffer = new byte[1024];
|
bin.transferTo(bout);
|
||||||
int bread = 0;
|
|
||||||
|
|
||||||
try {
|
|
||||||
while ((bread = bin.read(buffer)) >= 0) {
|
|
||||||
bout.write(buffer, 0, bread);
|
|
||||||
}
|
|
||||||
bin.close();
|
|
||||||
bout.flush();
|
|
||||||
bout.close();
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
notifyEvent(PrintJobEvent.JOB_FAILED);
|
notifyEvent(PrintJobEvent.JOB_FAILED);
|
||||||
throw new PrintException (e);
|
throw new PrintException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
|
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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -27,22 +27,19 @@ package sun.print;
|
|||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.BufferedOutputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
import javax.print.CancelablePrintJob;
|
import javax.print.CancelablePrintJob;
|
||||||
import javax.print.Doc;
|
import javax.print.Doc;
|
||||||
import javax.print.DocFlavor;
|
import javax.print.DocFlavor;
|
||||||
import javax.print.DocPrintJob;
|
|
||||||
import javax.print.PrintService;
|
import javax.print.PrintService;
|
||||||
import javax.print.PrintException;
|
import javax.print.PrintException;
|
||||||
import javax.print.event.PrintJobEvent;
|
import javax.print.event.PrintJobEvent;
|
||||||
@ -50,7 +47,6 @@ import javax.print.event.PrintJobListener;
|
|||||||
import javax.print.event.PrintJobAttributeListener;
|
import javax.print.event.PrintJobAttributeListener;
|
||||||
|
|
||||||
import javax.print.attribute.Attribute;
|
import javax.print.attribute.Attribute;
|
||||||
import javax.print.attribute.AttributeSet;
|
|
||||||
import javax.print.attribute.AttributeSetUtilities;
|
import javax.print.attribute.AttributeSetUtilities;
|
||||||
import javax.print.attribute.DocAttributeSet;
|
import javax.print.attribute.DocAttributeSet;
|
||||||
import javax.print.attribute.HashPrintJobAttributeSet;
|
import javax.print.attribute.HashPrintJobAttributeSet;
|
||||||
@ -437,18 +433,7 @@ public class Win32PrintJob implements CancelablePrintJob {
|
|||||||
|
|
||||||
if (mDestination != null) { // if destination attribute is set
|
if (mDestination != null) { // if destination attribute is set
|
||||||
try {
|
try {
|
||||||
FileOutputStream fos = new FileOutputStream(mDestination);
|
Files.copy(instream, Path.of(mDestination), StandardCopyOption.REPLACE_EXISTING);
|
||||||
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());
|
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
notifyEvent(PrintJobEvent.JOB_FAILED);
|
notifyEvent(PrintJobEvent.JOB_FAILED);
|
||||||
throw new PrintException(ioe.toString());
|
throw new PrintException(ioe.toString());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user