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;
}
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();
}
});
}
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();
}
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();
}
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);
}
Aggregations