Skip to content

Commit d9bd300

Browse files
author
Alan Bateman
committed
8374382: (aio) AsynchronousFileChannel writes wrong content using heap ByteBuffer when position != 0
Reviewed-by: jpai
1 parent 752f46d commit d9bd300

File tree

2 files changed

+191
-2
lines changed

2 files changed

+191
-2
lines changed

src/java.base/windows/classes/sun/nio/ch/WindowsAsynchronousFileChannelImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ public void run() {
452452
address = IOUtil.bufferAddress(dst) + pos;
453453
} else {
454454
buf = Util.getTemporaryDirectBuffer(rem);
455-
address = IOUtil.bufferAddress(buf) + pos;
455+
address = IOUtil.bufferAddress(buf);
456456
}
457457

458458
boolean pending = false;
@@ -640,7 +640,7 @@ public void run() {
640640
// temporarily restore position as we don't know how many bytes
641641
// will be written
642642
src.position(pos);
643-
address = IOUtil.bufferAddress(buf) + pos;
643+
address = IOUtil.bufferAddress(buf);
644644
}
645645

646646
try {
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8374382
27+
* @summary Test AsynchronousFileChannel.read/write with ByteBuffers and varied buffer positions
28+
* @run junit/othervm BufferPositions
29+
*/
30+
31+
import java.lang.foreign.Arena;
32+
import java.nio.ByteBuffer;
33+
import java.nio.channels.AsynchronousFileChannel;
34+
import java.nio.channels.CompletionHandler;
35+
import java.nio.file.Files;
36+
import java.nio.file.Path;
37+
import java.util.List;
38+
import java.util.concurrent.CountDownLatch;
39+
import java.util.concurrent.ThreadLocalRandom;
40+
import java.util.stream.Stream;
41+
import static java.nio.file.StandardOpenOption.*;
42+
43+
import org.junit.jupiter.params.ParameterizedTest;
44+
import org.junit.jupiter.params.provider.MethodSource;
45+
import static org.junit.jupiter.api.Assertions.*;
46+
47+
class BufferPositions {
48+
49+
private static final int BUF_SIZE = 32;
50+
51+
/**
52+
* The buffers to test.
53+
*/
54+
static Stream<ByteBuffer> buffers() {
55+
List<ByteBuffer> buffers = List.of(
56+
ByteBuffer.allocate(BUF_SIZE),
57+
ByteBuffer.allocateDirect(BUF_SIZE),
58+
ByteBuffer.wrap(new byte[BUF_SIZE]),
59+
Arena.global().allocate(BUF_SIZE).asByteBuffer(),
60+
Arena.ofAuto().allocate(BUF_SIZE).asByteBuffer(),
61+
Arena.ofShared().allocate(BUF_SIZE).asByteBuffer()
62+
);
63+
Stream<ByteBuffer> slices = buffers.stream()
64+
.map(bb -> bb.slice(1, bb.capacity() - 1));
65+
return Stream.concat(buffers.stream(), slices);
66+
}
67+
68+
/**
69+
* Test using an AsynchronousFileChannel to read bytes into the given buffer.
70+
*/
71+
@ParameterizedTest
72+
@MethodSource("buffers")
73+
void testRead(ByteBuffer bb) throws Exception {
74+
Path file = Files.createTempFile(Path.of("."), "test", "dat");
75+
76+
// populate temp file
77+
int size = bb.capacity();
78+
byte[] contents = new byte[size];
79+
for (int i = 0; i < size; i++) {
80+
contents[i] = (byte)(i + 1);
81+
}
82+
Files.write(file, contents);
83+
84+
// read bytes using the buffer as the destination and all possible buffer positions
85+
try (AsynchronousFileChannel ch = AsynchronousFileChannel.open(file, READ)) {
86+
for (int filePosition = 0; filePosition < size; filePosition++) {
87+
for (int bufPos = 0; bufPos < size; bufPos++) {
88+
// read one byte
89+
bb.position(bufPos);
90+
bb.limit(bufPos + 1);
91+
int n = read(ch, filePosition, bb);
92+
assertEquals(1, n);
93+
assertEquals(bufPos + 1, bb.position());
94+
assertEquals(contents[filePosition], bb.get(bufPos));
95+
}
96+
}
97+
}
98+
}
99+
100+
/**
101+
* Test using an AsynchronousFileChannel to write bytes from the given buffer.
102+
*/
103+
@ParameterizedTest
104+
@MethodSource("buffers")
105+
void testWrite(ByteBuffer bb) throws Exception {
106+
Path file = Files.createTempFile(Path.of("."), "test", "dat");
107+
108+
// populate temp file, all zeros
109+
int size = bb.capacity();
110+
byte[] contents = new byte[size];
111+
Files.write(file, contents);
112+
113+
// write bytes using the buffer as the source and all possible buffer positions
114+
try (AsynchronousFileChannel ch = AsynchronousFileChannel.open(file, READ, WRITE)) {
115+
for (int filePosition = 0; filePosition < size; filePosition++) {
116+
for (int bufPos = 0; bufPos < size; bufPos++) {
117+
// write one byte
118+
byte b = (byte) ThreadLocalRandom.current().nextInt();
119+
bb.position(bufPos);
120+
bb.limit(bufPos + 1);
121+
bb.put(bufPos, b);
122+
int n = write(ch, filePosition, bb);
123+
assertEquals(1, n);
124+
assertEquals(bufPos + 1, bb.position());
125+
assertEquals(b, bb.get(bufPos));
126+
127+
// check byte was written at the expected file position
128+
ByteBuffer dst = ByteBuffer.allocate(1);
129+
int nread = ch.read(dst, filePosition).get();
130+
assertEquals(1, nread);
131+
assertEquals(b, dst.get(0));
132+
}
133+
}
134+
}
135+
}
136+
137+
/**
138+
* Reads a byte from a channel into the given buffer, at the given file position.
139+
*/
140+
private int read(AsynchronousFileChannel ch, long position, ByteBuffer bb) throws Exception {
141+
if (ThreadLocalRandom.current().nextBoolean()) {
142+
return ch.read(bb, position).get();
143+
} else {
144+
var handler = new Handler<Integer>();
145+
ch.read(bb, position, null, handler);
146+
return handler.result();
147+
}
148+
}
149+
150+
/**
151+
* Writes a byte to a channel from the given buffer, at the given file position.
152+
*/
153+
private int write(AsynchronousFileChannel ch, long position, ByteBuffer bb) throws Exception {
154+
if (ThreadLocalRandom.current().nextBoolean()) {
155+
return ch.write(bb, position).get();
156+
} else {
157+
var handler = new Handler<Integer>();
158+
ch.write(bb, position, null, handler);
159+
return handler.result();
160+
}
161+
}
162+
163+
/**
164+
* CompletionHandler that defines a method to await the result of an I/O operation.
165+
*/
166+
private static class Handler<T> implements CompletionHandler<T, Void> {
167+
T result;
168+
Throwable ex;
169+
final CountDownLatch done = new CountDownLatch(1);
170+
171+
@Override
172+
public void completed(T result, Void att) {
173+
this.result = result;
174+
done.countDown();
175+
}
176+
177+
@Override
178+
public void failed(Throwable ex, Void att) {
179+
this.ex = ex;
180+
done.countDown();
181+
}
182+
183+
T result() throws InterruptedException {
184+
done.await();
185+
assertNull(ex);
186+
return result;
187+
}
188+
}
189+
}

0 commit comments

Comments
 (0)