1 /* 2 * Copyright (c) 2001, 2018, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.jfr.internal; 27 28 import java.io.BufferedInputStream; 29 import java.io.IOException; 30 import java.io.InputStream; 31 import java.util.ArrayList; 32 import java.util.Iterator; 33 import java.util.List; 34 35 final class ChunkInputStream extends InputStream { 36 private final Iterator<RepositoryChunk> chunks; 37 private RepositoryChunk currentChunk; 38 private InputStream stream; 39 40 ChunkInputStream(List<RepositoryChunk> chunks) throws IOException { 41 List<RepositoryChunk> l = new ArrayList<>(chunks.size()); 42 for (RepositoryChunk c : chunks) { 43 c.use(); // keep alive while we're reading. 44 l.add(c); 45 } 46 47 this.chunks = l.iterator(); 48 nextStream(); 49 } 50 51 @Override 52 public int available() throws IOException { 53 if (stream != null) { 54 return stream.available(); 55 } 56 return 0; 57 } 58 59 private boolean nextStream() throws IOException { 60 if (!nextChunk()) { 61 return false; 62 } 63 64 stream = new BufferedInputStream(SecuritySupport.newFileInputStream(currentChunk.getFile())); 65 return true; 66 } 67 68 private boolean nextChunk() { 69 if (!chunks.hasNext()) { 70 return false; 71 } 72 currentChunk = chunks.next(); 73 return true; 74 } 75 76 @Override 77 public int read() throws IOException { 78 while (true) { 79 if (stream != null) { 80 int r = stream.read(); 81 if (r != -1) { 82 return r; 83 } 84 stream.close(); 85 currentChunk.release(); 86 stream = null; 87 currentChunk = null; 88 } 89 if (!nextStream()) { 90 return -1; 91 } 92 } 93 } 94 95 @Override 96 public void close() throws IOException { 97 if (stream != null) { 98 stream.close(); 99 stream = null; 100 } 101 while (currentChunk != null) { 102 currentChunk.release(); 103 currentChunk = null; 104 if (!nextChunk()) { 105 return; 106 } 107 } 108 } 109 110 @Override 111 @SuppressWarnings("deprecation") 112 protected void finalize() throws Throwable { 113 super.finalize(); 114 close(); 115 } 116 }