Skip to content
Permalink
Browse files
8259956: jdk.jfr.internal.ChunkInputStream#available should return th…
…e sum of remaining available bytes

Reviewed-by: egahlin
  • Loading branch information
Denghui Dong authored and egahlin committed Feb 4, 2021
1 parent 06b33a0 commit e8ad8b35043d62fbefce473f35092d178d0fc20c
Showing with 64 additions and 3 deletions.
  1. +7 −3 src/jdk.jfr/share/classes/jdk/jfr/internal/ChunkInputStream.java
  2. +57 −0 test/jdk/jdk/jfr/api/consumer/TestChunkInputStreamAvailable.java
@@ -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 @@

final class ChunkInputStream extends InputStream {
private final Iterator<RepositoryChunk> chunks;
private long unstreamedSize = 0;
private RepositoryChunk currentChunk;
private InputStream stream;

@@ -42,6 +43,7 @@
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 @@

@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 @@ private boolean nextStream() throws IOException {
}

stream = new BufferedInputStream(SecuritySupport.newFileInputStream(currentChunk.getFile()));
unstreamedSize -= currentChunk.getSize();
return true;
}

@@ -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);
}
}
}
}

0 comments on commit e8ad8b3

Please sign in to comment.