use of java.nio.channels.ScatteringByteChannel in project NMEAParser by tvesalainen.
the class NetScanner method call.
@Override
public Set<String> call() throws Exception {
try (final ScatteringByteChannel channel = UnconnectedDatagramChannel.open(address, 10110, PortScanner.BUF_SIZE, true, false)) {
NMEAReader reader = new NMEAReader(address, matcher, channel, PortScanner.BUF_SIZE, this::onOk, this::onError);
reader.read();
} finally {
return fingerPrint;
}
}
use of java.nio.channels.ScatteringByteChannel in project kafka by apache.
the class NetworkReceiveTest method testBytesRead.
@Test
public void testBytesRead() throws IOException {
NetworkReceive receive = new NetworkReceive(128, "0");
assertEquals(0, receive.bytesRead());
ScatteringByteChannel channel = Mockito.mock(ScatteringByteChannel.class);
ArgumentCaptor<ByteBuffer> bufferCaptor = ArgumentCaptor.forClass(ByteBuffer.class);
Mockito.when(channel.read(bufferCaptor.capture())).thenAnswer(invocation -> {
bufferCaptor.getValue().putInt(128);
return 4;
}).thenReturn(0);
assertEquals(4, receive.readFrom(channel));
assertEquals(4, receive.bytesRead());
assertFalse(receive.complete());
Mockito.reset(channel);
Mockito.when(channel.read(bufferCaptor.capture())).thenAnswer(invocation -> {
bufferCaptor.getValue().put(TestUtils.randomBytes(64));
return 64;
});
assertEquals(64, receive.readFrom(channel));
assertEquals(68, receive.bytesRead());
assertFalse(receive.complete());
Mockito.reset(channel);
Mockito.when(channel.read(bufferCaptor.capture())).thenAnswer(invocation -> {
bufferCaptor.getValue().put(TestUtils.randomBytes(64));
return 64;
});
assertEquals(64, receive.readFrom(channel));
assertEquals(132, receive.bytesRead());
assertTrue(receive.complete());
}
use of java.nio.channels.ScatteringByteChannel in project NMEAParser by tvesalainen.
the class NMEAParser method parse.
public <I> void parse(I input, GPSClock gpsClock, Supplier origin, NMEAObserver data, AISObserver aisData) throws IOException {
NMEAChecksum checksum = getChecksum();
checksum.reset();
if (data == null) {
data = new AbstractNMEAObserver();
}
data.start(null);
data.setClock(gpsClock);
gpsClock.start(null);
data.commit("Set clock");
AISContext aisContext = null;
if (aisData != null) {
aisData.start(null);
aisData.setClock(gpsClock);
aisData.commit("Set clock");
aisContext = new AISContext(aisData);
}
try {
if (input instanceof ScatteringByteChannel) {
ScatteringByteChannel sbc = (ScatteringByteChannel) input;
parse(sbc, gpsClock, origin, data, aisContext);
} else {
if (input instanceof URL) {
URL url = (URL) input;
parse(url, gpsClock, origin, data, aisContext);
} else {
if (input instanceof String) {
String str = (String) input;
parse(str, gpsClock, origin, data, aisContext);
} else {
if (input instanceof InputStream) {
InputStream is = (InputStream) input;
parse(is, gpsClock, origin, data, aisContext);
} else {
throw new UnsupportedOperationException(input + " not supported as input");
}
}
}
}
} finally {
if (aisContext != null) {
aisContext.waitAndStopThreads();
}
}
}
use of java.nio.channels.ScatteringByteChannel in project netty by netty.
the class UnpooledTest method testUnmodifiableBuffer.
@Test
public void testUnmodifiableBuffer() throws Exception {
ByteBuf buf = unmodifiableBuffer(buffer(16));
try {
buf.discardReadBytes();
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
try {
buf.setByte(0, (byte) 0);
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
try {
buf.setBytes(0, EMPTY_BUFFER, 0, 0);
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
try {
buf.setBytes(0, EMPTY_BYTES, 0, 0);
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
try {
buf.setBytes(0, ByteBuffer.allocate(0));
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
try {
buf.setShort(0, (short) 0);
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
try {
buf.setMedium(0, 0);
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
try {
buf.setInt(0, 0);
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
try {
buf.setLong(0, 0);
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
InputStream inputStream = Mockito.mock(InputStream.class);
try {
buf.setBytes(0, inputStream, 0);
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
Mockito.verifyZeroInteractions(inputStream);
ScatteringByteChannel scatteringByteChannel = Mockito.mock(ScatteringByteChannel.class);
try {
buf.setBytes(0, scatteringByteChannel, 0);
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
Mockito.verifyZeroInteractions(scatteringByteChannel);
buf.release();
}
Aggregations