4774077: Use covariant return types in the NIO buffer hierarchy

Reviewed-by: psandoz, alanb, mr, darcy
This commit is contained in:
Richard Warburton 2014-10-29 14:10:34 +01:00
parent 9299e8a8a6
commit a065473cfc
10 changed files with 190 additions and 20 deletions

View File

@ -239,7 +239,7 @@ public abstract class Buffer {
* @throws IllegalArgumentException
* If the preconditions on <tt>newPosition</tt> do not hold
*/
public final Buffer position(int newPosition) {
public Buffer position(int newPosition) {
if ((newPosition > limit) || (newPosition < 0))
throw new IllegalArgumentException();
position = newPosition;
@ -270,7 +270,7 @@ public abstract class Buffer {
* @throws IllegalArgumentException
* If the preconditions on <tt>newLimit</tt> do not hold
*/
public final Buffer limit(int newLimit) {
public Buffer limit(int newLimit) {
if ((newLimit > capacity) || (newLimit < 0))
throw new IllegalArgumentException();
limit = newLimit;
@ -284,7 +284,7 @@ public abstract class Buffer {
*
* @return This buffer
*/
public final Buffer mark() {
public Buffer mark() {
mark = position;
return this;
}
@ -300,7 +300,7 @@ public abstract class Buffer {
* @throws InvalidMarkException
* If the mark has not been set
*/
public final Buffer reset() {
public Buffer reset() {
int m = mark;
if (m < 0)
throw new InvalidMarkException();
@ -325,7 +325,7 @@ public abstract class Buffer {
*
* @return This buffer
*/
public final Buffer clear() {
public Buffer clear() {
position = 0;
limit = capacity;
mark = -1;
@ -353,7 +353,7 @@ public abstract class Buffer {
*
* @return This buffer
*/
public final Buffer flip() {
public Buffer flip() {
limit = position;
position = 0;
mark = -1;
@ -375,7 +375,7 @@ public abstract class Buffer {
*
* @return This buffer
*/
public final Buffer rewind() {
public Buffer rewind() {
position = 0;
mark = -1;
return this;

View File

@ -208,4 +208,76 @@ public abstract class MappedByteBuffer
private native boolean isLoaded0(long address, long length, int pageCount);
private native void load0(long address, long length);
private native void force0(FileDescriptor fd, long address, long length);
// -- Covariant return type overrides
/**
* {@inheritDoc}
* @since 1.9
*/
@Override
public final MappedByteBuffer position(int newPosition) {
super.position(newPosition);
return this;
}
/**
* {@inheritDoc}
* @since 1.9
*/
@Override
public final MappedByteBuffer limit(int newLimit) {
super.limit(newLimit);
return this;
}
/**
* {@inheritDoc}
* @since 1.9
*/
@Override
public final MappedByteBuffer mark() {
super.mark();
return this;
}
/**
* {@inheritDoc}
* @since 1.9
*/
@Override
public final MappedByteBuffer reset() {
super.reset();
return this;
}
/**
* {@inheritDoc}
* @since 1.9
*/
@Override
public final MappedByteBuffer clear() {
super.clear();
return this;
}
/**
* {@inheritDoc}
* @since 1.9
*/
@Override
public final MappedByteBuffer flip() {
super.flip();
return this;
}
/**
* {@inheritDoc}
* @since 1.9
*/
@Override
public final MappedByteBuffer rewind() {
super.rewind();
return this;
}
}

View File

@ -1025,6 +1025,106 @@ public abstract class $Type$Buffer
return offset;
}
// -- Covariant return type overrides
/**
* {@inheritDoc}
* @since 1.9
*/
@Override
public
#if[!byte]
final
#end[!byte]
$Type$Buffer position(int newPosition) {
super.position(newPosition);
return this;
}
/**
* {@inheritDoc}
* @since 1.9
*/
@Override
public
#if[!byte]
final
#end[!byte]
$Type$Buffer limit(int newLimit) {
super.limit(newLimit);
return this;
}
/**
* {@inheritDoc}
* @since 1.9
*/
@Override
public
#if[!byte]
final
#end[!byte]
$Type$Buffer mark() {
super.mark();
return this;
}
/**
* {@inheritDoc}
* @since 1.9
*/
@Override
public
#if[!byte]
final
#end[!byte]
$Type$Buffer reset() {
super.reset();
return this;
}
/**
* {@inheritDoc}
* @since 1.9
*/
@Override
public
#if[!byte]
final
#end[!byte]
$Type$Buffer clear() {
super.clear();
return this;
}
/**
* {@inheritDoc}
* @since 1.9
*/
@Override
public
#if[!byte]
final
#end[!byte]
$Type$Buffer flip() {
super.flip();
return this;
}
/**
* {@inheritDoc}
* @since 1.9
*/
@Override
public
#if[!byte]
final
#end[!byte]
$Type$Buffer rewind() {
super.rewind();
return this;
}
/**
* Compacts this buffer&nbsp;&nbsp;<i>(optional operation)</i>.
*

View File

@ -560,7 +560,7 @@ final class CipherBox {
+ newLen);
hd.encodeBuffer(
(ByteBuffer)bb.duplicate().position(pos), System.out);
bb.duplicate().position(pos), System.out);
} catch (IOException e) { }
}
@ -790,7 +790,7 @@ final class CipherBox {
// The padding data should be filled with the padding length value.
int[] results = checkPadding(
(ByteBuffer)bb.duplicate().position(offset + newLen),
bb.duplicate().position(offset + newLen),
(byte)(padLen & 0xFF));
if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
if (results[0] != 0) { // padding data has invalid bytes

View File

@ -349,8 +349,7 @@ final class EngineInputRecord extends InputRecord {
/*
* Copy data out of buffer, it's ready to go.
*/
ByteBuffer netBB = (ByteBuffer)
(ByteBuffer.allocate(len).put(buf, 0, len).flip());
ByteBuffer netBB = ByteBuffer.allocate(len).put(buf, 0, len).flip();
engine.writer.putOutboundDataSync(netBB);
}

View File

@ -113,9 +113,7 @@ final class EngineOutputRecord extends OutputRecord {
/*
* Copy data out of buffer, it's ready to go.
*/
ByteBuffer netBB = (ByteBuffer)
ByteBuffer.allocate(len).put(buf, off, len).flip();
ByteBuffer netBB = ByteBuffer.allocate(len).put(buf, off, len).flip();
writer.putOutboundData(netBB);
}

View File

@ -245,9 +245,10 @@ public class Finalize {
* write to fc2 - when fos1 is gc'ed and finalizer is run,
* write to fc2 should not fail
*/
bb = ByteBuffer.allocateDirect(data.length);
bb = bb.put(data);
bb = (ByteBuffer) bb.flip();
bb = ByteBuffer.allocateDirect(data.length)
.put(data)
.flip();
ret = fc2.write(bb);
System.out.println("Wrote:" + ret + " bytes to fc2");
fc2.close();

View File

@ -35,7 +35,7 @@ import java.nio.charset.*;
public class Flush {
private static byte[] contents(ByteBuffer bb) {
byte[] contents = new byte[bb.position()];
((ByteBuffer)(bb.duplicate().flip())).get(contents);
bb.duplicate().flip().get(contents);
return contents;
}

View File

@ -150,7 +150,7 @@ public class TestUTF_16 {
if (CoderResult.OVERFLOW !=
Charset.forName("UTF_16")
.newDecoder()
.decode((ByteBuffer)(ByteBuffer.allocate(4)
.decode((ByteBuffer.allocate(4)
.put(new byte[]
{(byte)0xd8,(byte)0x00,
(byte)0xdc,(byte)0x01})

View File

@ -184,7 +184,7 @@ public class TestUTF_32 {
if (CoderResult.OVERFLOW !=
Charset.forName("UTF_32")
.newDecoder()
.decode((ByteBuffer)(ByteBuffer.allocate(4)
.decode((ByteBuffer.allocate(4)
.put(new byte[]
{(byte)0,(byte)1, (byte)0,(byte)01})
.flip()),