2013-06-10 17:00:16 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2013 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project 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.
|
|
|
|
*/
|
|
|
|
package io.netty.channel.nio;
|
|
|
|
|
|
|
|
import java.nio.channels.SelectionKey;
|
|
|
|
import java.util.AbstractSet;
|
2016-11-23 22:50:42 +01:00
|
|
|
import java.util.Arrays;
|
2013-06-10 17:00:16 +02:00
|
|
|
import java.util.Iterator;
|
2018-09-01 08:10:02 +02:00
|
|
|
import java.util.NoSuchElementException;
|
2013-06-10 17:00:16 +02:00
|
|
|
|
|
|
|
final class SelectedSelectionKeySet extends AbstractSet<SelectionKey> {
|
|
|
|
|
2016-11-23 22:50:42 +01:00
|
|
|
SelectionKey[] keys;
|
|
|
|
int size;
|
2013-06-10 17:00:16 +02:00
|
|
|
|
|
|
|
SelectedSelectionKeySet() {
|
2016-11-23 22:50:42 +01:00
|
|
|
keys = new SelectionKey[1024];
|
2013-06-10 17:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean add(SelectionKey o) {
|
|
|
|
if (o == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-23 22:50:42 +01:00
|
|
|
keys[size++] = o;
|
|
|
|
if (size == keys.length) {
|
|
|
|
increaseCapacity();
|
2013-06-10 17:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-01 08:33:32 +02:00
|
|
|
@Override
|
|
|
|
public boolean remove(Object o) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean contains(Object o) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-10 17:00:16 +02:00
|
|
|
@Override
|
|
|
|
public int size() {
|
2016-11-23 22:50:42 +01:00
|
|
|
return size;
|
2013-06-10 17:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-01 08:10:02 +02:00
|
|
|
public Iterator<SelectionKey> iterator() {
|
|
|
|
return new Iterator<SelectionKey>() {
|
|
|
|
private int idx;
|
2013-06-10 17:00:16 +02:00
|
|
|
|
2018-09-01 08:10:02 +02:00
|
|
|
@Override
|
|
|
|
public boolean hasNext() {
|
|
|
|
return idx < size;
|
|
|
|
}
|
2013-06-10 17:00:16 +02:00
|
|
|
|
2018-09-01 08:10:02 +02:00
|
|
|
@Override
|
|
|
|
public SelectionKey next() {
|
|
|
|
if (!hasNext()) {
|
|
|
|
throw new NoSuchElementException();
|
|
|
|
}
|
|
|
|
return keys[idx++];
|
|
|
|
}
|
2018-09-01 08:59:08 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void remove() {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
2018-09-01 08:10:02 +02:00
|
|
|
};
|
2013-06-10 17:00:16 +02:00
|
|
|
}
|
2016-11-23 22:50:42 +01:00
|
|
|
|
|
|
|
void reset() {
|
|
|
|
reset(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset(int start) {
|
|
|
|
Arrays.fill(keys, start, size, null);
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void increaseCapacity() {
|
|
|
|
SelectionKey[] newKeys = new SelectionKey[keys.length << 1];
|
|
|
|
System.arraycopy(keys, 0, newKeys, 0, size);
|
|
|
|
keys = newKeys;
|
|
|
|
}
|
2013-06-10 17:00:16 +02:00
|
|
|
}
|