8343039: Remove jdk.internal.misc.InternalLock and usages from java.io

Reviewed-by: liach, alanb
This commit is contained in:
Brian Burkhalter 2024-11-15 16:11:34 +00:00
parent 3c38ed4128
commit 0b9b82af03
19 changed files with 548 additions and 1789 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2024, 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
@ -44,8 +44,6 @@ import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Arrays;
import jdk.internal.misc.InternalLock;
public class StreamDecoder extends Reader {
private static final int MIN_BYTE_BUFFER_SIZE = 32;
@ -121,151 +119,95 @@ public class StreamDecoder extends Reader {
return read0();
}
private int read0() throws IOException {
Object lock = this.lock;
if (lock instanceof InternalLock locker) {
locker.lock();
try {
return lockedRead0();
} finally {
locker.unlock();
}
} else {
synchronized (lock) {
return lockedRead0();
}
}
}
@SuppressWarnings("fallthrough")
private int lockedRead0() throws IOException {
// Return the leftover char, if there is one
if (haveLeftoverChar) {
haveLeftoverChar = false;
return leftoverChar;
}
private int read0() throws IOException {
synchronized (lock) {
// Return the leftover char, if there is one
if (haveLeftoverChar) {
haveLeftoverChar = false;
return leftoverChar;
}
// Convert more bytes
char[] cb = new char[2];
int n = read(cb, 0, 2);
switch (n) {
case -1:
return -1;
case 2:
leftoverChar = cb[1];
haveLeftoverChar = true;
// FALL THROUGH
case 1:
return cb[0];
default:
assert false : n;
return -1;
// Convert more bytes
char[] cb = new char[2];
int n = read(cb, 0, 2);
switch (n) {
case -1:
return -1;
case 2:
leftoverChar = cb[1];
haveLeftoverChar = true;
// FALL THROUGH
case 1:
return cb[0];
default:
assert false : n;
return -1;
}
}
}
public int read(char[] cbuf, int offset, int length) throws IOException {
Object lock = this.lock;
if (lock instanceof InternalLock locker) {
locker.lock();
try {
return lockedRead(cbuf, offset, length);
} finally {
locker.unlock();
synchronized (lock) {
int off = offset;
int len = length;
ensureOpen();
if ((off < 0) || (off > cbuf.length) || (len < 0) ||
((off + len) > cbuf.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
}
} else {
synchronized (lock) {
return lockedRead(cbuf, offset, length);
if (len == 0)
return 0;
int n = 0;
if (haveLeftoverChar) {
// Copy the leftover char into the buffer
cbuf[off] = leftoverChar;
off++; len--;
haveLeftoverChar = false;
n = 1;
if ((len == 0) || !implReady())
// Return now if this is all we can produce w/o blocking
return n;
}
if (len == 1) {
// Treat single-character array reads just like read()
int c = read0();
if (c == -1)
return (n == 0) ? -1 : n;
cbuf[off] = (char)c;
return n + 1;
}
// Read remaining characters
int nr = implRead(cbuf, off, off + len);
// At this point, n is either 1 if a leftover character was read,
// or 0 if no leftover character was read. If n is 1 and nr is -1,
// indicating EOF, then we don't return their sum as this loses data.
return (nr < 0) ? (n == 1 ? 1 : nr) : (n + nr);
}
}
private int lockedRead(char[] cbuf, int offset, int length) throws IOException {
int off = offset;
int len = length;
ensureOpen();
if ((off < 0) || (off > cbuf.length) || (len < 0) ||
((off + len) > cbuf.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
}
if (len == 0)
return 0;
int n = 0;
if (haveLeftoverChar) {
// Copy the leftover char into the buffer
cbuf[off] = leftoverChar;
off++; len--;
haveLeftoverChar = false;
n = 1;
if ((len == 0) || !implReady())
// Return now if this is all we can produce w/o blocking
return n;
}
if (len == 1) {
// Treat single-character array reads just like read()
int c = read0();
if (c == -1)
return (n == 0) ? -1 : n;
cbuf[off] = (char)c;
return n + 1;
}
// Read remaining characters
int nr = implRead(cbuf, off, off + len);
// At this point, n is either 1 if a leftover character was read,
// or 0 if no leftover character was read. If n is 1 and nr is -1,
// indicating EOF, then we don't return their sum as this loses data.
return (nr < 0) ? (n == 1 ? 1 : nr) : (n + nr);
}
public boolean ready() throws IOException {
Object lock = this.lock;
if (lock instanceof InternalLock locker) {
locker.lock();
try {
return lockedReady();
} finally {
locker.unlock();
}
} else {
synchronized (lock) {
return lockedReady();
}
synchronized (lock) {
ensureOpen();
return haveLeftoverChar || implReady();
}
}
private boolean lockedReady() throws IOException {
ensureOpen();
return haveLeftoverChar || implReady();
}
public void close() throws IOException {
Object lock = this.lock;
if (lock instanceof InternalLock locker) {
locker.lock();
synchronized (lock) {
if (closed)
return;
try {
lockedClose();
implClose();
} finally {
locker.unlock();
closed = true;
}
} else {
synchronized (lock) {
lockedClose();
}
}
}
private void lockedClose() throws IOException {
if (closed)
return;
try {
implClose();
} finally {
closed = true;
}
}
@ -274,25 +216,12 @@ public class StreamDecoder extends Reader {
}
public void fillZeroToPosition() throws IOException {
Object lock = this.lock;
if (lock instanceof InternalLock locker) {
locker.lock();
try {
lockedFillZeroToPosition();
} finally {
locker.unlock();
}
} else {
synchronized (lock) {
lockedFillZeroToPosition();
}
synchronized (lock) {
Arrays.fill(bb.array(), bb.arrayOffset(),
bb.arrayOffset() + bb.position(), (byte)0);
}
}
private void lockedFillZeroToPosition() {
Arrays.fill(bb.array(), bb.arrayOffset(), bb.arrayOffset() + bb.position(), (byte)0);
}
// -- Charset-based stream decoder impl --
private final Charset cs;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2024, 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
@ -37,7 +37,6 @@ import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import jdk.internal.misc.InternalLock;
public final class StreamEncoder extends Writer {
@ -97,28 +96,14 @@ public final class StreamEncoder extends Writer {
}
public void flushBuffer() throws IOException {
Object lock = this.lock;
if (lock instanceof InternalLock locker) {
locker.lock();
try {
lockedFlushBuffer();
} finally {
locker.unlock();
}
} else {
synchronized (lock) {
lockedFlushBuffer();
}
synchronized (lock) {
if (isOpen())
implFlushBuffer();
else
throw new IOException("Stream closed");
}
}
private void lockedFlushBuffer() throws IOException {
if (isOpen())
implFlushBuffer();
else
throw new IOException("Stream closed");
}
public void write(int c) throws IOException {
char[] cbuf = new char[1];
cbuf[0] = (char) c;
@ -126,30 +111,16 @@ public final class StreamEncoder extends Writer {
}
public void write(char[] cbuf, int off, int len) throws IOException {
Object lock = this.lock;
if (lock instanceof InternalLock locker) {
locker.lock();
try {
lockedWrite(cbuf, off, len);
} finally {
locker.unlock();
}
} else {
synchronized (lock) {
lockedWrite(cbuf, off, len);
}
}
}
private void lockedWrite(char[] cbuf, int off, int len) throws IOException {
ensureOpen();
if ((off < 0) || (off > cbuf.length) || (len < 0) ||
synchronized (lock) {
ensureOpen();
if ((off < 0) || (off > cbuf.length) || (len < 0) ||
((off + len) > cbuf.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
implWrite(cbuf, off, len);
}
implWrite(cbuf, off, len);
}
public void write(String str, int off, int len) throws IOException {
@ -164,73 +135,31 @@ public final class StreamEncoder extends Writer {
public void write(CharBuffer cb) throws IOException {
int position = cb.position();
try {
Object lock = this.lock;
if (lock instanceof InternalLock locker) {
locker.lock();
try {
lockedWrite(cb);
} finally {
locker.unlock();
}
} else {
synchronized (lock) {
lockedWrite(cb);
}
synchronized (lock) {
ensureOpen();
implWrite(cb);
}
} finally {
cb.position(position);
}
}
private void lockedWrite(CharBuffer cb) throws IOException {
ensureOpen();
implWrite(cb);
}
public void flush() throws IOException {
Object lock = this.lock;
if (lock instanceof InternalLock locker) {
locker.lock();
try {
lockedFlush();
} finally {
locker.unlock();
}
} else {
synchronized (lock) {
lockedFlush();
}
synchronized (lock) {
ensureOpen();
implFlush();
}
}
private void lockedFlush() throws IOException {
ensureOpen();
implFlush();
}
public void close() throws IOException {
Object lock = this.lock;
if (lock instanceof InternalLock locker) {
locker.lock();
synchronized (lock) {
if (closed)
return;
try {
lockedClose();
implClose();
} finally {
locker.unlock();
closed = true;
}
} else {
synchronized (lock) {
lockedClose();
}
}
}
private void lockedClose() throws IOException {
if (closed)
return;
try {
implClose();
} finally {
closed = true;
}
}