Path: blob/master/src/java.base/share/classes/sun/security/provider/FileInputStreamPool.java
41159 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.provider;2627import java.io.*;28import java.lang.ref.ReferenceQueue;29import java.lang.ref.WeakReference;30import java.util.concurrent.ConcurrentHashMap;31import java.util.concurrent.ConcurrentMap;3233/**34* A pool of {@code InputStream}s opened from distinct files. Only a single35* instance is ever opened from the same file. This is used to read special36* infinite files like {@code /dev/random} where the current file pointer is not37* relevant, so multiple readers can share the same file descriptor and38* consequently the same {@code InputStream}.39*/40class FileInputStreamPool {4142/**43* a pool of: StreamRef -> UnclosableInputStream -> FileInputStream(s)44*/45private static final ConcurrentMap<File, StreamRef> pool =46new ConcurrentHashMap<>();4748/**49* a reference queue of cleared StreamRef(s)50*/51private static final ReferenceQueue<UnclosableInputStream> refQueue =52new ReferenceQueue<>();5354/**55* This method opens an underlying {@link java.io.FileInputStream} for a56* given {@code file} and returns a wrapper over it. The wrapper is shared57* among multiple readers of the same {@code file} and ignores58* {@link java.io.InputStream#close()} requests. The underlying stream is59* closed when all references to the wrapper are relinquished.60*61* @param file the file to be opened for reading.62* @return a shared {@link java.io.InputStream} instance opened from given63* file.64* @throws FileNotFoundException if the file does not exist, is a directory65* rather than a regular file, or for some66* other reason cannot be opened for reading.67* @throws SecurityException if a security manager exists and its68* <code>checkRead</code> method denies read69* access to the file.70*/71static InputStream getInputStream(File file) throws IOException {7273// expunge any cleared references74StreamRef oldRref;75while ((oldRref = (StreamRef) refQueue.poll()) != null) {76pool.remove(oldRref.file, oldRref);77}7879// canonicalize the path80// (this also checks the read permission on the file if SecurityManager81// is present, so no checking is needed later when we just return the82// already opened stream)83File cfile = file.getCanonicalFile();8485// check if it exists in pool86oldRref = pool.get(cfile);87UnclosableInputStream oldStream = (oldRref == null)88? null89: oldRref.get();90StreamRef newRef = null;91UnclosableInputStream newStream = null;9293// retry loop94while (true) {95if (oldStream != null) {96// close our optimistically opened stream 1st (if we opened it)97if (newStream != null) {98try {99newStream.getWrappedStream().close();100} catch (IOException ignore) {101// can't do anything here102}103}104// return it105return oldStream;106} else {107// we need to open new stream optimistically (if not already)108if (newStream == null) {109newStream = new UnclosableInputStream(110new FileInputStream(cfile));111newRef = new StreamRef(cfile, newStream, refQueue);112}113// either try to install newRef or replace oldRef with newRef114if (oldRref == null) {115oldRref = pool.putIfAbsent(cfile, newRef);116} else {117oldRref = pool.replace(cfile, oldRref, newRef)118? null119: pool.get(cfile);120}121if (oldRref == null) {122// success123return newStream;124} else {125// lost race126oldStream = oldRref.get();127// another loop128}129}130}131}132133private static class StreamRef extends WeakReference<UnclosableInputStream> {134final File file;135136StreamRef(File file,137UnclosableInputStream stream,138ReferenceQueue<UnclosableInputStream> refQueue) {139super(stream, refQueue);140this.file = file;141}142}143144private static final class UnclosableInputStream extends FilterInputStream {145UnclosableInputStream(InputStream in) {146super(in);147}148149@Override150public void close() throws IOException {151// Ignore close attempts since underlying InputStream is shared.152}153154InputStream getWrappedStream() {155return in;156}157}158}159160161