1 /*
   2  * Copyright (c) 2012, 2019, 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.FileNotFoundException;
  29 import java.io.IOException;
  30 import java.nio.ByteBuffer;
  31 import java.nio.channels.FileChannel;
  32 import java.nio.channels.ReadableByteChannel;
  33 import java.util.ArrayList;
  34 import java.util.Iterator;
  35 import java.util.List;
  36 
  37 final class ChunksChannel implements ReadableByteChannel {
  38     private final Iterator<RepositoryChunk> chunks;
  39     private RepositoryChunk                 current;
  40     private ReadableByteChannel             channel;
  41 
  42     public ChunksChannel(List<RepositoryChunk> chunks) throws IOException {
  43         if (chunks.isEmpty()) {
  44             throw new FileNotFoundException("No chunks");
  45         }
  46         List<RepositoryChunk> l = new ArrayList<>(chunks.size());
  47         for (RepositoryChunk c : chunks) {
  48             c.use(); // keep alive while we're reading.
  49             l.add(c);
  50         }
  51         this.chunks = l.iterator();
  52         nextChannel();
  53     }
  54 
  55     private boolean nextChunk() {
  56         if (!chunks.hasNext()) {
  57             return false;
  58         }
  59         current = chunks.next();
  60         return true;
  61     }
  62 
  63     private boolean nextChannel() throws IOException {
  64         if (!nextChunk()) {
  65             return false;
  66         }
  67 
  68         channel = current.newChannel();
  69         return true;
  70     }
  71 
  72     @Override
  73     public int read(ByteBuffer dst) throws IOException {
  74         for (;;) {
  75             if (channel != null) {
  76                 assert current != null;
  77                 int r = channel.read(dst);
  78                 if (r != -1) {
  79                     return r;
  80                 }
  81                 channel.close();
  82                 current.release();
  83                 channel = null;
  84                 current = null;
  85             }
  86             if (!nextChannel()) {
  87                 return -1;
  88             }
  89         }
  90     }
  91 
  92     public long transferTo(FileChannel out) throws IOException {
  93         long pos = 0;
  94         for (;;) {
  95             if (channel != null) {
  96                 assert current != null;
  97 
  98                 long rem = current.getSize();
  99 
 100                 while (rem > 0) {
 101                     long n = Math.min(rem, 1024 * 1024);
 102                     long w = out.transferFrom(channel, pos, n);
 103                     pos += w;
 104                     rem -= w;
 105                 }
 106 
 107                 channel.close();
 108                 current.release();
 109 
 110                 channel = null;
 111                 current = null;
 112             }
 113             if (!nextChannel()) {
 114                 return pos;
 115             }
 116         }
 117     }
 118 
 119     @Override
 120     public void close() throws IOException {
 121         if (channel != null) {
 122             channel.close();
 123             channel = null;
 124         }
 125         while (current != null) {
 126             current.release();
 127             current = null;
 128             if (!nextChunk()) {
 129                 return;
 130             }
 131         }
 132     }
 133 
 134     @Override
 135     public boolean isOpen() {
 136         return channel != null;
 137     }
 138 
 139     @Override
 140     @SuppressWarnings("deprecation")
 141     protected void finalize() throws Throwable {
 142         super.finalize();
 143         close();
 144     }
 145 }