Search in sources :

Example 11 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class NioChannelFactory method packeted.

public static <C extends PacketedNioChannel> C packeted(C channel0) {
    C channel = buffered(channel0);
    channel.onReceive.wire(new Sink<>() {

        private Bytes received = Bytes.empty;

        public void sink(Bytes message) {
            received = received.append(message);
            int size = received.size();
            if (4 <= size) {
                int end = 4 + NetUtil.bytesToInt(received.range(0, 4));
                if (end <= size) {
                    Bytes in = received.range(4, end);
                    received = received.range(end);
                    channel.onReceivePacket.fire(in);
                }
            }
        }
    });
    return channel;
}
Also used : Bytes(suite.primitive.Bytes)

Example 12 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class As method utf8decode.

public static Outlet<Chars> utf8decode(Outlet<Bytes> bytesOutlet) {
    Source<Bytes> source = bytesOutlet.source();
    return Outlet.of(new Source<>() {

        private BytesBuilder bb = new BytesBuilder();

        public Chars source() {
            Chars chars;
            while ((chars = decode()).size() == 0) {
                Bytes bytes = source.source();
                if (bytes != null)
                    bb.append(bytes);
                else if (bb.size() == 0)
                    return null;
                else
                    return Fail.t();
            }
            return chars;
        }

        private Chars decode() {
            Bytes bytes = bb.toBytes();
            CharsBuilder cb = new CharsBuilder();
            int s = 0;
            while (s < bytes.size()) {
                int b0 = Byte.toUnsignedInt(bytes.get(s++));
                int ch, e;
                if (b0 < 0x80) {
                    ch = b0;
                    e = s;
                } else if (b0 < 0xE0) {
                    ch = b0 & 0x1F;
                    e = s + 1;
                } else if (b0 < 0xF0) {
                    ch = b0 & 0x0F;
                    e = s + 2;
                } else if (b0 < 0xF8) {
                    ch = b0 & 0x07;
                    e = s + 3;
                } else if (b0 < 0xFC) {
                    ch = b0 & 0x03;
                    e = s + 4;
                } else if (b0 < 0xFE) {
                    ch = b0 & 0x01;
                    e = s + 5;
                } else
                    throw new RuntimeException();
                if (e <= bytes.size()) {
                    while (s < e) {
                        int b = Byte.toUnsignedInt(bytes.get(s++));
                        if ((b & 0xC0) == 0x80)
                            ch = (ch << 6) + (b & 0x3F);
                        else
                            Fail.t();
                    }
                    cb.append((char) ch);
                } else
                    break;
            }
            bb = new BytesBuilder();
            bb.append(bytes.range(s));
            return cb.toChars();
        }
    });
}
Also used : Bytes(suite.primitive.Bytes) CharsBuilder(suite.primitive.Chars.CharsBuilder) Chars(suite.primitive.Chars) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Example 13 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class TextUtil method diff.

public List<Pair<Bytes, Bytes>> diff(Bytes bytesx, Bytes bytesy) {
    Lccs lccs = new Lccs();
    Pair<Segment, Segment> diff = lccs.lccs(bytesx, bytesy);
    Segment sx = diff.t0, sy = diff.t1;
    int x0 = 0, x1 = sx.start, x2 = sx.end, xx = bytesx.size();
    int y0 = 0, y1 = sy.start, y2 = sy.end, yx = bytesy.size();
    Bytes common = bytesx.range(x1, x2);
    if (!sx.isEmpty() && !sy.isEmpty()) {
        List<Pair<Bytes, Bytes>> patch = new ArrayList<>();
        patch.addAll(diff(bytesx.range(x0, x1), bytesy.range(y0, y1)));
        patch.add(Pair.of(common, common));
        patch.addAll(diff(bytesx.range(x2, xx), bytesy.range(y2, yx)));
        return patch;
    } else if (!bytesx.isEmpty() || !bytesy.isEmpty())
        return List.of(Pair.of(bytesx, bytesy));
    else
        return List.of();
}
Also used : Bytes(suite.primitive.Bytes) Lccs(suite.lcs.Lccs) ArrayList(java.util.ArrayList) Pair(suite.adt.pair.Pair)

Example 14 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class TextUtil method merge.

public List<Pair<Bytes, Bytes>> merge(List<Pair<Bytes, Bytes>> pairsx, List<Pair<Bytes, Bytes>> pairsy, boolean isDetectSameChanges) throws ConflictException {
    boolean isEmptyx = pairsx.isEmpty();
    boolean isEmptyy = pairsy.isEmpty();
    if (!isEmptyx || !isEmptyy) {
        Pair<Bytes, Bytes> phx = !isEmptyx ? pairsx.get(0) : Pair.of(Bytes.empty, Bytes.empty);
        Pair<Bytes, Bytes> phy = !isEmptyy ? pairsy.get(0) : Pair.of(Bytes.empty, Bytes.empty);
        List<Pair<Bytes, Bytes>> ptx = !isEmptyx ? List_.right(pairsx, 1) : pairsx;
        List<Pair<Bytes, Bytes>> pty = !isEmptyy ? List_.right(pairsy, 1) : pairsy;
        int c = min(phx.t0.size(), phy.t0.size());
        Bytes commonx = phx.t0.range(0, c);
        Bytes commony = phy.t0.range(0, c);
        if (Objects.equals(commonx, commony)) {
            int s0, s1;
            Pair<Bytes, Bytes> pair;
            List<Pair<Bytes, Bytes>> pairs;
            if (// 
            isDetectSameChanges && // 
            phx.t0 != phx.t1 && // 
            phy.t0 != phy.t1 && // 
            0 < (s0 = detectSame(phx.t0, phy.t0)) && 0 < (s1 = detectSame(phx.t1, phy.t1))) {
                pair = Pair.of(phx.t0.range(0, s0), phx.t1.range(0, s1));
                pairs = merge(// 
                cons(Pair.of(phx.t0.range(s0), phx.t1.range(s1)), ptx), // 
                cons(Pair.of(phy.t0.range(s0), phy.t1.range(s1)), pty), isDetectSameChanges);
            } else if (phx.t0 != phx.t1) {
                pair = phx;
                pairs = merge(ptx, cons(skip(phy, c), pty), isDetectSameChanges);
            } else if (phy.t0 != phy.t1) {
                pair = phy;
                pairs = merge(cons(skip(phx, c), ptx), pty, isDetectSameChanges);
            } else {
                pair = Pair.of(commonx, commonx);
                pairs = merge(cons(skip(phx, c), ptx), cons(skip(phy, c), pty), isDetectSameChanges);
            }
            return cons(pair, pairs);
        } else
            throw new ConflictException();
    } else
        return List.of();
}
Also used : Bytes(suite.primitive.Bytes) Pair(suite.adt.pair.Pair)

Example 15 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class NioDispatcherTest method testRequestResponse.

@Test
public void testRequestResponse() throws IOException, InterruptedException {
    RequestResponseMatcher matcher = new RequestResponseMatcher();
    ThreadPoolExecutor executor = Thread_.newExecutor();
    Iterate<Bytes> handler = request -> request;
    NioDispatcher<RequestResponseNioChannel> dispatcher = new NioDispatcherImpl<>(() -> NioChannelFactory.requestResponse(new RequestResponseNioChannel(), matcher, executor, handler));
    dispatcher.start();
    try (Closeable closeServer = dispatcher.listen(5151)) {
        InetAddress localHost = InetAddress.getLocalHost();
        InetSocketAddress address = new InetSocketAddress(localHost, 5151);
        RequestResponseNioChannel client = dispatcher.connect(address);
        for (String s : new String[] { "ABC", "WXYZ", "" }) {
            byte[] bs = s.getBytes(Constants.charset);
            Bytes request = Bytes.of(bs);
            Bytes response = matcher.requestForResponse(client, request);
            assertEquals(request, response);
            System.out.println("Request '" + s + "' is okay");
        }
    } finally {
        dispatcher.stop();
    }
    executor.awaitTermination(0, TimeUnit.SECONDS);
}
Also used : OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) Socket(java.net.Socket) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Constants(suite.Constants) Source(suite.util.FunUtil.Source) Bytes(suite.primitive.Bytes) Thread_(suite.util.Thread_) IOException(java.io.IOException) Test(org.junit.Test) To(suite.util.To) NioChannel(suite.net.nio.NioChannelFactory.NioChannel) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) TimeUnit(java.util.concurrent.TimeUnit) BufferedNioChannel(suite.net.nio.NioChannelFactory.BufferedNioChannel) Iterate(suite.util.FunUtil.Iterate) Charset(java.nio.charset.Charset) Closeable(java.io.Closeable) BufferedReader(java.io.BufferedReader) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) RequestResponseNioChannel(suite.net.nio.NioChannelFactory.RequestResponseNioChannel) InetSocketAddress(java.net.InetSocketAddress) Closeable(java.io.Closeable) RequestResponseNioChannel(suite.net.nio.NioChannelFactory.RequestResponseNioChannel) Bytes(suite.primitive.Bytes) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

Bytes (suite.primitive.Bytes)56 Test (org.junit.Test)18 BytesBuilder (suite.primitive.Bytes.BytesBuilder)8 IOException (java.io.IOException)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Pair (suite.adt.pair.Pair)4 OutputStream (java.io.OutputStream)3 Path (java.nio.file.Path)3 To (suite.util.To)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Constants (suite.Constants)2 Amd64Interpret (suite.assembler.Amd64Interpret)2 Condition (suite.concurrent.Condition)2 Extent (suite.file.ExtentAllocator.Extent)2 JournalledPageFile (suite.file.JournalledPageFile)2 ImperativeCompiler (suite.ip.ImperativeCompiler)2 DataInput_ (suite.util.DataInput_)2