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 source channel.

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

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

Parameters:
delegate the channel to wrap
 
     public GatedStreamSourceChannel(final StreamSourceChannel delegate) {
         this. = delegate;
     }
 
     @SuppressWarnings("unused")
     private int state;
 
     private static final int FLAG_GATE_OPEN = 1 << 0;
     private static final int FLAG_READS_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 shutdownReads() 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)) {
                 .shutdownReads();
             }
             if (allAreSet(val)) {
                 .wakeupReads();
             }
         }
     }
 
     public boolean isGateOpen() {
         return allAreSet();
     }
 
     public XnioWorker getWorker() {
         return .getWorker();
    }
    @Override
    public XnioIoThread getIoThread() {
        return .getIoThread();
    }
    @Override
    public long transferTo(long positionlong countFileChannel targetthrows IOException {
        int val = ;
        if (anyAreSet(val)) {
            return -1;
        }
        if (anyAreClear(val)) {
            return 0;
        }
        return .transferTo(positioncounttarget);
    }
    @Override
    public long transferTo(long countByteBuffer throughBufferStreamSinkChannel targetthrows IOException {
        int val = ;
        if (anyAreSet(val)) {
            return -1;
        }
        if (anyAreClear(val)) {
            return 0;
        }
        return .transferTo(countthroughBuffertarget);
    }
    @Override
    public long read(ByteBuffer[] dstsint offsetint lengththrows IOException {
        int val = ;
        if (anyAreSet(val)) {
            return -1;
        }
        if (anyAreClear(val)) {
            return 0;
        }
        return .read(dstsoffsetlength);
    }
    @Override
    public long read(ByteBuffer[] dststhrows IOException {
        int val = ;
        if (anyAreSet(val)) {
            return -1;
        }
        if (anyAreClear(val)) {
            return 0;
        }
        return .read(dsts);
    }
    @Override
    public int read(ByteBuffer dstthrows IOException {
        int val = ;
        if (anyAreSet(val)) {
            return -1;
        }
        if (anyAreClear(val)) {
            return 0;
        }
        return .read(dst);
    }
    @Override
    public void suspendReads() {
        if (anyAreSet()) {
            .suspendReads();
        } else {
             &= ~;
        }
    }
    @Override
    public void resumeReads() {
        if (anyAreSet()) {
            .resumeReads();
        } else {
             |= ;
        }
    }
    @Override
    public boolean isReadResumed() {
        if (anyAreSet()) {
            return .isReadResumed();
        } else {
            return anyAreSet();
        }
    }
    @Override
    public void wakeupReads() {
        if (anyAreSet()) {
            .resumeReads();
        } else {
             |= ;
            getIoThread().execute(new Runnable() {
                @Override
                public void run() {
                    ChannelListeners.invokeChannelListener(GatedStreamSourceChannel.this.get());
                }
            });
        }
    }
    @Override
    public void shutdownReads() throws IOException {
        if (anyAreSet()) {
            .shutdownReads();
        } else {
             |= ;
        }
    }
    @Override
    public void awaitReadable() throws IOException {
        if (anyAreSet()) {
            .awaitReadable();
        } else {
            throw new IllegalStateException();
        }
    }
    @Override
    public void awaitReadable(long timeTimeUnit timeUnitthrows IOException {
        if (anyAreSet()) {
            .awaitReadable(timetimeUnit);
        } else {
            throw new IllegalStateException();
        }
    }
    @Override
    public XnioExecutor getReadThread() {
        return .getIoThread();
    }
    @Override
    public ChannelListener.Setter<? extends StreamSourceChannelgetReadSetter() {
        return ;
    }
    public ChannelListener.Setter<? extends StreamSourceChannelgetCloseSetter() {
        return ;
    }
    public void close() throws IOException {
        if (allAreSet()) {
            return;
        }
         |= ;
        if (anyAreSet()) {
            .close();
        }
    }
    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 StreamSourceChannel getChannel() {
        return allAreSet() ?  : this;
    }
New to GrepCode? Check out our FAQ X