8259956: jdk.jfr.internal.ChunkInputStream#available should return the sum of remaining available bytes

Reviewed-by: egahlin
This commit is contained in:
Denghui Dong 2021-02-04 09:22:01 +00:00 committed by Erik Gahlin
parent 06b33a0ad7
commit e8ad8b3504
2 changed files with 64 additions and 3 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 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
@ -34,6 +34,7 @@ import java.util.List;
final class ChunkInputStream extends InputStream {
private final Iterator<RepositoryChunk> chunks;
private long unstreamedSize = 0;
private RepositoryChunk currentChunk;
private InputStream stream;
@ -42,6 +43,7 @@ final class ChunkInputStream extends InputStream {
for (RepositoryChunk c : chunks) {
c.use(); // keep alive while we're reading.
l.add(c);
unstreamedSize += c.getSize();
}
this.chunks = l.iterator();
@ -50,10 +52,11 @@ final class ChunkInputStream extends InputStream {
@Override
public int available() throws IOException {
long total = unstreamedSize;
if (stream != null) {
return stream.available();
total += stream.available();
}
return 0;
return total <= Integer.MAX_VALUE ? (int) total : Integer.MAX_VALUE;
}
private boolean nextStream() throws IOException {
@ -62,6 +65,7 @@ final class ChunkInputStream extends InputStream {
}
stream = new BufferedInputStream(SecuritySupport.newFileInputStream(currentChunk.getFile()));
unstreamedSize -= currentChunk.getSize();
return true;
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (c) 2021, Alibaba Group Holding Limited. 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Alibaba designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* @test TestChunkInputStreamAvailable
* @key jfr
* @requires vm.hasJFR
* @library /test/lib
* @run main/othervm jdk.jfr.api.consumer.TestChunkInputStreamAvailable
*/
package jdk.jfr.api.consumer;
import java.io.InputStream;
import jdk.jfr.Recording;
import jdk.test.lib.Asserts;
public class TestChunkInputStreamAvailable {
public static void main(String[] args) throws Exception {
try (Recording r = new Recording()) {
r.start();
try (Recording s = new Recording()) {
s.start();
s.stop();
}
r.stop();
try (InputStream stream = r.getStream(null, null)) {
int left = stream.available();
Asserts.assertEquals(r.getSize(), (long) left);
while (stream.read() != -1) {
left--;
Asserts.assertEquals(left, stream.available());
}
Asserts.assertEquals(0, left);
}
}
}
}