Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * Licensed 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.
  */
 
 package io.undertow.channels;
 
 
 
 import static org.xnio.Bits.allAreClear;
 import static org.xnio.Bits.allAreSet;
 import static org.xnio.Bits.anyAreClear;
 import static org.xnio.Bits.anyAreSet;

A 'gated' stream sink channel.

This channel has a gate which starts of closed. When the gate is closed writes will return 0. When the gate is opened writes will resume as normal.

Author(s):
David M. Lloyd
 
 public final class GatedStreamSinkChannel implements StreamSinkChannel {
     private final StreamSinkChannel delegate;
Construct a new instance.

Parameters:
delegate the channel to wrap
 
     public GatedStreamSinkChannel(final StreamSinkChannel delegate) {
         this. = delegate;
     }
 
     @SuppressWarnings("unused")
     private int state;
 
     private static final int FLAG_GATE_OPEN = 1 << 0;
     private static final int FLAG_WRITES_RESUMED = 1 << 1;
     private static final int FLAG_CLOSE_REQUESTED = 1 << 2;
     private static final int FLAG_CLOSED = 1 << 3;

    
Open the gate and allow data to flow. Once opened, the gate cannot be closed other than closing the channel.

If the shutdownWrites() or close() method has already been called this will result it in being invoked on the delegate.

 
     public void openGate() throws IOException {
         int val = ;
         if (allAreSet(val)) {
             return;
         }
          |= ;
         if (allAreSet(val)) {
             .close();
         } else {
             if (allAreSet(val)) {
                 .shutdownWrites();
             }
             if (allAreSet(val)) {
                 .wakeupWrites();
             }
         }
     }
 
     public boolean isGateOpen() {
         return allAreSet();
     }
 
     public XnioWorker getWorker() {
        return .getWorker();
    }
    @Override
    public XnioIoThread getIoThread() {
        return .getIoThread();
    }
    public XnioExecutor getWriteThread() {
        return .getWriteThread();
    }
    public ChannelListener.Setter<? extends StreamSinkChannelgetWriteSetter() {
        return ;
    }
    public ChannelListener.Setter<? extends StreamSinkChannelgetCloseSetter() {
        return ;
    }
    @Override
    public int writeFinal(ByteBuffer srcthrows IOException {
        if (handleGate()) {
            return 0;
        }
        return .writeFinal(src);
    }
    @Override
    public long writeFinal(ByteBuffer[] srcsint offsetint lengththrows IOException {
        if (handleGate()) {
            return 0;
        }
        return .writeFinal(srcsoffsetlength);
    }
    @Override
    public long writeFinal(ByteBuffer[] srcsthrows IOException {
        if (handleGate()) {
            return 0;
        }
        return .writeFinal(srcs);
    }
    public int write(final ByteBuffer srcthrows IOException {
        if (handleGate()) {
            return 0;
        }
        return .write(src);
    }
    public long write(final ByteBuffer[] srcsthrows IOException {
        return write(srcs, 0, srcs.length);
    }
    public long write(final ByteBuffer[] srcsfinal int offsetfinal int lengththrows IOException {
        if (handleGate()) {
            return 0;
        }
        return .write(srcsoffsetlength);
    }
    private boolean handleGate() throws ClosedChannelException {
        int val = ;
        if (anyAreSet(val)) {
            throw new ClosedChannelException();
        }
        if (anyAreClear(val)) {
            return true;
        }
        return false;
    }
    public long transferFrom(final FileChannel srcfinal long positionfinal long countthrows IOException {
        if (handleGate()) {
            return 0;
        }
        return .transferFrom(srcpositioncount);
    }
    public long transferFrom(final StreamSourceChannel sourcefinal long countfinal ByteBuffer throughBufferthrows IOException {
        if (handleGate()) {
            return 0;
        }
        return .transferFrom(sourcecountthroughBuffer);
    }
    public boolean flush() throws IOException {
        if (anyAreClear()) {
            return false;
        }
        if (anyAreSet()) {
            throw new ClosedChannelException();
        }
        if (anyAreSet()) {
            boolean result = .flush();
            if (result) {
                 |= ;
            }
            return result;
        }
        return .flush();
    }
    public void suspendWrites() {
        if (anyAreSet()) {
            .suspendWrites();
        } else {
             &= ~;
        }
    }
    public void resumeWrites() {
        if (anyAreSet()) {
            .resumeWrites();
        } else {
             |= ;
        }
    }
    public boolean isWriteResumed() {
        if (anyAreSet()) {
            return .isWriteResumed();
        } else {
            return anyAreSet();
        }
    }
    public void wakeupWrites() {
        if (anyAreSet()) {
            .wakeupWrites();
        } else {
             |= ;
            getIoThread().execute(new Runnable() {
                @Override
                public void run() {
                    ChannelListeners.invokeChannelListener(GatedStreamSinkChannel.this.get());
                }
            });
        }
    }
    public void shutdownWrites() throws IOException {
         |= ;
        if (anyAreSet()) {
            .shutdownWrites();
        }
    }
    public void close() throws IOException {
        if (allAreSet()) {
            return;
        }
         |= ;
        if (anyAreSet()) {
            .close();
        }
    }
    public void awaitWritable() throws IOException {
        if (allAreClear()) {
            throw new IllegalStateException();//we don't allow this, as it results in thread safety issues
        }
        .awaitWritable();
    }
    public void awaitWritable(final long timefinal TimeUnit timeUnitthrows IOException {
        if (allAreClear()) {
            throw new IllegalStateException();//we don't allow this, as it results in thread safety issues
        }
        .awaitWritable(timetimeUnit);
    }
    public boolean isOpen() {
        return allAreClear();
    }
    public boolean supportsOption(final Option<?> option) {
        return false;
    }
    public <T> T getOption(final Option<T> optionthrows IOException {
        return null;
    }
    public <T> T setOption(final Option<T> optionfinal T valuethrows IllegalArgumentExceptionIOException {
        return null;
    }

    
Get the underlying channel if the gate is open, else return this channel.

Returns:
the underlying channel, or this channel if the gate is not open
    public StreamSinkChannel getChannel() {
        return allAreSet() ?  : this;
    }
New to GrepCode? Check out our FAQ X