Search in sources :

Example 1 with ScatteringByteChannel

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;
    }
}
Also used : NMEAReader(org.vesalainen.nmea.router.NMEAReader) ScatteringByteChannel(java.nio.channels.ScatteringByteChannel)

Example 2 with ScatteringByteChannel

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());
}
Also used : Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) ScatteringByteChannel(java.nio.channels.ScatteringByteChannel) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) TestUtils(org.apache.kafka.test.TestUtils) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) IOException(java.io.IOException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ByteBuffer(java.nio.ByteBuffer) ByteBuffer(java.nio.ByteBuffer) ScatteringByteChannel(java.nio.channels.ScatteringByteChannel) Test(org.junit.jupiter.api.Test)

Example 3 with ScatteringByteChannel

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();
        }
    }
}
Also used : InputStream(java.io.InputStream) AISContext(org.vesalainen.parsers.nmea.ais.AISContext) ScatteringByteChannel(java.nio.channels.ScatteringByteChannel) URL(java.net.URL)

Example 4 with ScatteringByteChannel

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();
}
Also used : InputStream(java.io.InputStream) ScatteringByteChannel(java.nio.channels.ScatteringByteChannel) Test(org.junit.jupiter.api.Test)

Aggregations

ScatteringByteChannel (java.nio.channels.ScatteringByteChannel)4 InputStream (java.io.InputStream)2 Test (org.junit.jupiter.api.Test)2 IOException (java.io.IOException)1 URL (java.net.URL)1 ByteBuffer (java.nio.ByteBuffer)1 TestUtils (org.apache.kafka.test.TestUtils)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1 ArgumentCaptor (org.mockito.ArgumentCaptor)1 Mockito (org.mockito.Mockito)1 NMEAReader (org.vesalainen.nmea.router.NMEAReader)1 AISContext (org.vesalainen.parsers.nmea.ais.AISContext)1