All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.java.truevfs.kernel.impl.LockOutputService Maven / Gradle / Ivy

/*
 * Copyright © 2005 - 2021 Schlichtherle IT Services.
 * All rights reserved. Use is subject to license terms.
 */
package net.java.truevfs.kernel.impl;

import edu.umd.cs.findbugs.annotations.DischargesObligation;
import net.java.truecommons.cio.*;
import net.java.truecommons.io.LockOutputStream;
import net.java.truecommons.io.LockSeekableChannel;

import javax.annotation.WillCloseWhenClosed;
import javax.annotation.concurrent.ThreadSafe;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.SeekableByteChannel;
import java.util.Iterator;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Decorates another output service to allow concurrent access which is synchronized by a
 * {@link java.util.concurrent.locks.Lock}.
 *
 * @param  the type of the entries in the decorated output service.
 * @author Christian Schlichtherle
 * @see LockInputService
 */
@ThreadSafe
class LockOutputService extends DecoratingOutputService implements LockAspect {

    private final Lock lock = new ReentrantLock();

    LockOutputService(@WillCloseWhenClosed OutputService output) {
        super(output);
    }


    @Override
    public Lock lock() {
        return lock;
    }

    @DischargesObligation
    @Override
    public void close() throws IOException {
        locked(new Op() {

            @Override
            public Object call() throws IOException {
                container.close();
                return null;
            }
        });
    }

    @Override
    public int size() {
        return locked(new Op() {

            @Override
            public Integer call() {
                return container.size();
            }
        });
    }

    @Override
    public Iterator iterator() {
        throw new UnsupportedOperationException("The returned iterator would not be thread-safe!");
    }

    @Override
    public E entry(String name) {
        return locked(new Op() {

            @Override
            public E call() {
                return container.entry(name);
            }
        });
    }

    @Override
    public OutputSocket output(E entry) {
        return new AbstractOutputSocket() {

            private final OutputSocket socket = container.output(entry);

            @Override
            public E target() throws IOException {
                return locked(new Op() {

                    @Override
                    public E call() throws IOException {
                        return socket.target();
                    }
                });
            }

            @Override
            public OutputStream stream(InputSocket peer) throws IOException {
                return new LockOutputStream(lock, locked(new Op() {

                    @Override
                    public OutputStream call() throws IOException {
                        return socket.stream(peer);
                    }
                }));
            }

            @Override
            public SeekableByteChannel channel(InputSocket peer) throws IOException {
                return new LockSeekableChannel(lock, locked(new Op() {

                    @Override
                    public SeekableByteChannel call() throws IOException {
                        return socket.channel(peer);
                    }
                }));
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy