Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.apache.lucene.mockfile.filterfs;
- /*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- import java.io.FilterInputStream;
- import java.io.FilterOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.nio.channels.AsynchronousFileChannel;
- import java.nio.channels.FileChannel;
- import java.nio.channels.SeekableByteChannel;
- import java.nio.file.FileSystem;
- import java.nio.file.OpenOption;
- import java.nio.file.Path;
- import java.nio.file.attribute.FileAttribute;
- import java.util.Collections;
- import java.util.IdentityHashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- import java.util.concurrent.ExecutorService;
- public class LeakFS extends FilterFileSystemProvider {
- private final Map<Object,Exception> openHandles = Collections.synchronizedMap(new IdentityHashMap<>());
- public LeakFS(FileSystem delegate) {
- super("leakfs://", delegate);
- }
- @Override
- public InputStream newInputStream(Path path, OpenOption... options) throws IOException {
- InputStream stream = new FilterInputStream(super.newInputStream(path, options)) {
- @Override
- public void close() throws IOException {
- openHandles.remove(this);
- super.close();
- }
- @Override
- public String toString() {
- return path.toString();
- }
- };
- openHandles.put(stream, new Exception());
- return stream;
- }
- @Override
- public OutputStream newOutputStream(Path path, OpenOption... options) throws IOException {
- OutputStream stream = new FilterOutputStream(super.newOutputStream(path, options)) {
- @Override
- public void close() throws IOException {
- openHandles.remove(this);
- super.close();
- }
- @Override
- public String toString() {
- return path.toString();
- }
- };
- openHandles.put(stream, new Exception());
- return stream;
- }
- @Override
- public FileChannel newFileChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
- return super.newFileChannel(path, options, attrs);
- }
- @Override
- public AsynchronousFileChannel newAsynchronousFileChannel(Path path, Set<? extends OpenOption> options, ExecutorService executor, FileAttribute<?>... attrs) throws IOException {
- return super.newAsynchronousFileChannel(path, options, executor, attrs);
- }
- @Override
- public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
- return super.newByteChannel(path, options, attrs);
- }
- @Override
- public synchronized void onClose() {
- if (!openHandles.isEmpty()) {
- // print the first one as its very verbose otherwise
- Exception cause = null;
- Iterator<Exception> stacktraces = openHandles.values().iterator();
- if (stacktraces.hasNext()) {
- cause = stacktraces.next();
- }
- throw new RuntimeException("file handle leaks: " + openHandles.keySet(), cause);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement