Java Code Examples for net.openhft.chronicle.bytes.Bytes#readRemaining()
The following examples show how to use
net.openhft.chronicle.bytes.Bytes#readRemaining() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FixSaxParser.java From SAXophone with GNU Lesser General Public License v3.0 | 6 votes |
@Override public void parse(Bytes bytes) { long limit = bytes.readLimit(), limit2 = limit; while (limit2 > bytes.readPosition() && bytes.readByte(limit2 - 1) != FIELD_TERMINATOR) limit2--; bytes.readLimit(limit2); while (bytes.readRemaining() > 0) { long fieldNum = bytes.parseLong(); long pos = bytes.readPosition(); searchForTheEndOfField(bytes); long end = bytes.readPosition() - 1; bytes.readLimit(end); bytes.readPosition(pos); handler.completeMessage(bytes); handler.onField(fieldNum, bytes); bytes.readLimit(limit); bytes.readPosition(end + 1); } bytes.readLimit(limit); bytes.readPosition(limit2); }
Example 2
Source File: JsonParser.java From SAXophone with GNU Lesser General Public License v3.0 | 6 votes |
boolean on() throws IOException { Bytes buf = lexer.outBuf; long bufPos = lexer.outPos; long bufLen = lexer.outLen; boolean borrowBuf = buf.readPosition() != bufPos || buf.readRemaining() != bufLen; long pos = 0, lim = 0; if (borrowBuf) { pos = buf.readPosition(); lim = buf.readLimit(); //buf.clear(); buf.readLimit(bufPos + bufLen); buf.readPosition(bufPos); } boolean go = apply(value(buf)); if (borrowBuf) { //buf.clear(); buf.readLimit(lim); buf.readPosition(pos); } return go; }
Example 3
Source File: TcpChannelHub.java From Chronicle-Network with Apache License 2.0 | 6 votes |
private void logToStandardOutMessageSent(@NotNull final WireOut wire, @NotNull final ByteBuffer outBuffer) { if (!YamlLogging.showClientWrites()) return; @NotNull Bytes<?> bytes = wire.bytes(); try { if (bytes.readRemaining() > 0) LOG.info(((!YamlLogging.title.isEmpty()) ? "### " + YamlLogging .title + "\n" : "") + "" + YamlLogging.writeMessage() + (YamlLogging.writeMessage().isEmpty() ? "" : "\n\n") + "sends:\n\n" + "```yaml\n" + Wires.fromSizePrefixedBlobs(bytes) + "```"); YamlLogging.title = ""; YamlLogging.writeMessage(""); } catch (Exception e) { Jvm.warn().on(TcpChannelHub.class, Bytes.toString(bytes), e); } }
Example 4
Source File: BytesForTesting.java From Chronicle-Salt with Apache License 2.0 | 5 votes |
static void checkPseudoRandom(Bytes bytes, long size) throws java.nio.BufferUnderflowException { bytes.readPositionRemaining(0, size); int count = 0; while (bytes.readRemaining() > 7) { count += Long.bitCount(bytes.readLong()); } assertEquals(size * 4, count, size); }
Example 5
Source File: Lexer.java From SAXophone with GNU Lesser General Public License v3.0 | 5 votes |
private TokenType lexComment(Bytes jsonText) { int c; TokenType tok = COMMENT; if (jsonText.readRemaining() == 0) return EOF; c = readChar(jsonText); /* either slash or star expected */ if (c == '/') { /* now we throw away until end of line */ do { if (jsonText.readRemaining() == 0) return EOF; c = readChar(jsonText); } while (c != '\n'); } else if (c == '*') { /* now we throw away until end of comment */ for (;;) { if (jsonText.readRemaining() == 0) return EOF; c = readChar(jsonText); if (c == '*') { if (jsonText.readRemaining() == 0) return EOF; c = readChar(jsonText); if (c == '/') { break; } else { unreadChar(jsonText); } } } } else { error = INVALID_CHAR; tok = ERROR; } return tok; }
Example 6
Source File: Lexer.java From SAXophone with GNU Lesser General Public License v3.0 | 5 votes |
/** process a variable length utf8 encoded codepoint. * * @return * STRING - if valid utf8 char was parsed and offset was advanced * EOF - if end of input was hit before validation could complete * ERROR - if invalid utf8 was encountered * * NOTE: on error the offset will point to the first char of the * invalid utf8 */ private TokenType lexUtf8Char(Bytes jsonText, int curChar) { if (curChar <= 0x7f) { /* single byte */ return STRING; } else if ((curChar >> 5) == 0x6) { /* two byte */ if (jsonText.readRemaining() == 0) return EOF; curChar = readChar(jsonText); if ((curChar >> 6) == 0x2) return STRING; } else if ((curChar >> 4) == 0x0e) { /* three byte */ if (jsonText.readRemaining() == 0) return EOF; curChar = readChar(jsonText); if ((curChar >> 6) == 0x2) { if (jsonText.readRemaining() == 0) return EOF; curChar = readChar(jsonText); if ((curChar >> 6) == 0x2) return STRING; } } else if ((curChar >> 3) == 0x1e) { /* four byte */ if (jsonText.readRemaining() == 0) return EOF; curChar = readChar(jsonText); if ((curChar >> 6) == 0x2) { if (jsonText.readRemaining() == 0) return EOF; curChar = readChar(jsonText); if ((curChar >> 6) == 0x2) { if (jsonText.readRemaining() == 0) return EOF; curChar = readChar(jsonText); if ((curChar >> 6) == 0x2) return STRING; } } } return ERROR; }
Example 7
Source File: TimedEchoHandler.java From Chronicle-Network with Apache License 2.0 | 5 votes |
@Override public void process(@NotNull final Bytes in, @NotNull final Bytes out, T nc) { if (in.readRemaining() == 0) return; long toWrite = Math.min(in.readRemaining(), out.writeRemaining()); out.write(in, in.readPosition(), toWrite); out.writeLong(System.nanoTime()); in.readSkip(toWrite); }
Example 8
Source File: EchoHandler.java From Chronicle-Network with Apache License 2.0 | 5 votes |
@Override public void process(@NotNull final Bytes in, @NotNull final Bytes out, T nc) { // System.out.println(in.readRemaining()); if (in.readRemaining() == 0) return; // System.out.println("P start " + in.toDebugString()); long toWrite = Math.min(in.readRemaining(), out.writeRemaining()); out.write(in, in.readPosition(), toWrite); in.readSkip(toWrite); // System.out.println("... P End " + in.toDebugString()); }
Example 9
Source File: NonClusteredSslIntegrationTest.java From Chronicle-Network with Apache License 2.0 | 5 votes |
@Override public void process(@NotNull final Bytes in, @NotNull final Bytes out, final StubNetworkContext nc) { latch.countDown(); try { if (nc.isAcceptor() && in.readRemaining() != 0) { final int magic = in.readInt(); if (magic != 0xFEDCBA98) throw new IllegalStateException("Invalid magic number " + Integer.toHexString(magic)); final long received = in.readLong(); final int len = in.readInt(); final byte[] tmp = new byte[len]; in.read(tmp); if (DEBUG) { if (len > 10) { System.out.printf("%s received payload of length %d%n", label, len); System.out.println(in); } else { System.out.printf("%s received [%d] %d/%s%n", label, tmp.length, received, new String(tmp, StandardCharsets.US_ASCII)); } } operationCount++; } else if (!nc.isAcceptor()) { if (System.currentTimeMillis() > lastSent + 100L) { out.writeInt(0xFEDCBA98); out.writeLong((counter++)); final String payload = "ping-" + (counter - 1); out.writeInt(payload.length()); out.write(payload.getBytes(StandardCharsets.US_ASCII)); if (DEBUG) { System.out.printf("%s sent [%d] %d/%s%n", label, payload.length(), counter - 1, payload); } operationCount++; lastSent = System.currentTimeMillis(); } } } catch (RuntimeException e) { System.err.printf("Exception in %s: %s/%s%n", label, e.getClass().getSimpleName(), e.getMessage()); e.printStackTrace(); } }
Example 10
Source File: WireTcpHandler.java From Chronicle-Network with Apache License 2.0 | 5 votes |
private void ensureCapacity() { @NotNull final Bytes<?> bytes = inWire.bytes(); if (bytes.readRemaining() >= 4) { final long pos = bytes.readPosition(); int length = bytes.readInt(pos); final long size = pos + Wires.SPB_HEADER_SIZE * 2 + Wires.lengthOf(length); if (size > bytes.realCapacity()) { resizeInWire(size); } } }
Example 11
Source File: BatchSignAndVerifyEd25519Test.java From Chronicle-Salt with Apache License 2.0 | 5 votes |
@Test public void signAndVerify() { Bytes privateKeyBuffer = null; Bytes secretKeyBuffer = null; Bytes privateOrSecret = bft.fromHex(privateOrSecretKey); if (privateOrSecret.readRemaining() == Ed25519.SECRET_KEY_LENGTH) { secretKeyBuffer = privateOrSecret; } else { privateKeyBuffer = privateOrSecret; } Bytes publicKeyBuffer = bft.fromHex(publicKey); if (secretKeyBuffer == null) { secretKeyBuffer = bft.bytesWithZeros(Ed25519.SECRET_KEY_LENGTH); Bytes tmpPublicKeyBuffer = bft.bytesWithZeros(Ed25519.PUBLIC_KEY_LENGTH); Ed25519.privateToPublicAndSecret(tmpPublicKeyBuffer, secretKeyBuffer, privateKeyBuffer); assertEquals(publicKeyBuffer.toHexString(), tmpPublicKeyBuffer.toHexString()); } Bytes messageBuffer = bft.fromHex(message); Bytes signExpectedBuffer; if (signExpected.length() == 128) { signExpectedBuffer = Bytes.wrapForRead(DatatypeConverter.parseHexBinary(signExpected + message)); } else { signExpectedBuffer = Bytes.wrapForRead(DatatypeConverter.parseHexBinary(signExpected)); } Bytes signedMsgBuffer = bft.fromHex(Ed25519.SIGNATURE_LENGTH, message); signedMsgBuffer.writePosition(0); Ed25519.sign(signedMsgBuffer, messageBuffer, secretKeyBuffer); assertEquals(signExpectedBuffer.toHexString(), signedMsgBuffer.toHexString()); signedMsgBuffer.readPosition(0); publicKeyBuffer.readPositionRemaining(0, Ed25519.PUBLIC_KEY_LENGTH); assertTrue(Ed25519.verify(signedMsgBuffer, publicKeyBuffer)); }
Example 12
Source File: WireTcpHandler.java From Chronicle-Network with Apache License 2.0 | 4 votes |
@Override public void process(@NotNull final Bytes in, @NotNull final Bytes out, final T nc) { if (isClosed()) return; WireType wireType = wireType(); if (wireType == null) wireType = in.readByte(in.readPosition() + 4) < 0 ? WireType.BINARY : WireType.TEXT; checkWires(in, out, wireType); // we assume that if any bytes were in lastOutBytesRemaining the sc.write() would have been // called and this will fail, if the other end has lost its connection if (outWire.bytes().writePosition() != lastWritePosition) { onBytesWritten(); // NOTE you can not use remaining as the buffer maybe resized writeBps += (lastWritePosition - outWire.bytes().writePosition()); } socketPollCount++; bytesReadCount += (in.readRemaining() - lastReadRemaining); final long now = System.currentTimeMillis(); if (now > lastMonitor + 10000) { final NetworkStatsListener<T> networkStatsListener = this.nc.networkStatsListener(); if (networkStatsListener != null && !networkStatsListener.isClosed()) { if (lastMonitor == 0) { networkStatsListener.onNetworkStats(0, 0, 0); } else { networkStatsListener.onNetworkStats(writeBps / 10, bytesReadCount / 10, socketPollCount / 10); writeBps = bytesReadCount = socketPollCount = 0; } } lastMonitor = now; } if (publisher != null) publisher.applyAction(outWire); if (in.readRemaining() >= SIZE_OF_SIZE) onRead0(); else onWrite(outWire); lastWritePosition = outWire.bytes().writePosition(); lastReadRemaining = inWire.bytes().readRemaining(); }
Example 13
Source File: Lexer.java From SAXophone with GNU Lesser General Public License v3.0 | 4 votes |
/** * Lex a string. * * @return * STRING - lex of string was successful. offset points to terminating '"'. * EOF - end of text was encountered before we could complete the lex. * ERROR - embedded in the string were unallowable chars. offset * points to the offending char */ private TokenType lexString(Bytes jsonText) { TokenType tok = ERROR; boolean hasEscapes = false; finish_string_lex: for (;;) { int curChar; /* now jump into a faster scanning routine to skip as much * of the buffers as possible */ { if (bufInUse && buf.readRemaining() > 0) { stringScan(buf); } else if (jsonText.readRemaining() > 0) { stringScan(jsonText); } } if (jsonText.readRemaining() == 0) { tok = EOF; break; } curChar = readChar(jsonText); /* quote terminates */ if (curChar == '"') { tok = STRING; break; } /* backslash escapes a set of control chars, */ else if (curChar == '\\') { hasEscapes = true; if (jsonText.readRemaining() == 0) { tok = EOF; break; } /* special case \\u */ curChar = readChar(jsonText); if (curChar == 'u') { for (int i = 0; i < 4; i++) { if (jsonText.readRemaining() == 0) { tok = EOF; break finish_string_lex; } curChar = readChar(jsonText); if ((CHAR_LOOKUP_TABLE[curChar] & VHC) == 0) { /* back up to offending char */ unreadChar(jsonText); error = STRING_INVALID_HEX_CHAR; break finish_string_lex; } } } else if ((CHAR_LOOKUP_TABLE[curChar] & VEC) == 0) { /* back up to offending char */ unreadChar(jsonText); error = STRING_INVALID_ESCAPED_CHAR; break; } } /* when not validating UTF8 it's a simple table lookup to determine * if the present character is invalid */ else if((CHAR_LOOKUP_TABLE[curChar] & IJC) != 0) { /* back up to offending char */ unreadChar(jsonText); error = STRING_INVALID_JSON_CHAR; break; } /* when in validate UTF8 mode we need to do some extra work */ else if (validateUTF8) { TokenType t = lexUtf8Char(jsonText, curChar); if (t == EOF) { tok = EOF; break; } else if (t == ERROR) { error = STRING_INVALID_UTF8; break; } } /* accept it, and move on */ } /* tell our buddy, the parser, whether he needs to process this string again */ if (hasEscapes && tok == STRING) { tok = STRING_WITH_ESCAPES; } return tok; }
Example 14
Source File: Lexer.java From SAXophone with GNU Lesser General Public License v3.0 | 4 votes |
private TokenType lexNumber(Bytes jsonText) { /** XXX: numbers are the only entities in json that we must lex * _beyond_ in order to know that they are complete. There * is an ambiguous case for integers at EOF. */ int c; TokenType tok = INTEGER; if (jsonText.readRemaining() == 0) return EOF; c = readChar(jsonText); /* optional leading minus */ if (c == '-') { if (jsonText.readRemaining() == 0) return EOF; c = readChar(jsonText); } /* a single zero, or a series of integers */ if (c == '0') { if (jsonText.readRemaining() == 0) return EOF; c = readChar(jsonText); } else if (c >= '1' && c <= '9') { do { if (jsonText.readRemaining() == 0) return EOF; c = readChar(jsonText); } while (c >= '0' && c <= '9'); } else { unreadChar(jsonText); error = MISSING_INTEGER_AFTER_MINUS; return ERROR; } /* optional fraction (indicates this is floating point) */ if (c == '.') { boolean readSome = false; if (jsonText.readRemaining() == 0) return EOF; c = readChar(jsonText); while (c >= '0' && c <= '9') { readSome = true; if (jsonText.readRemaining() == 0) return EOF; c = readChar(jsonText); } if (!readSome) { unreadChar(jsonText); error = MISSING_INTEGER_AFTER_DECIMAL; return ERROR; } tok = DOUBLE; } /* optional exponent (indicates this is floating point) */ if (c == 'e' || c == 'E') { if (jsonText.readRemaining() == 0) return EOF; c = readChar(jsonText); /* optional sign */ if (c == '+' || c == '-') { if (jsonText.readRemaining() == 0) return EOF; c = readChar(jsonText); } if (c >= '0' && c <= '9') { do { if (jsonText.readRemaining() == 0) return EOF; c = readChar(jsonText); } while (c >= '0' && c <= '9'); } else { unreadChar(jsonText); error = MISSING_INTEGER_AFTER_EXPONENT; return ERROR; } tok = DOUBLE; } /* we always go "one too far" */ unreadChar(jsonText); return tok; }
Example 15
Source File: Unescaper.java From SAXophone with GNU Lesser General Public License v3.0 | 4 votes |
static void decode(StringBuilder buf, Bytes str) { long len = str.readRemaining(); long pos = str.readPosition(); int beg = 0; long end = pos; char codePoint; while (end < len) { if (str.readUnsignedByte(end) == '\\') { buf.append(str, beg, (int) (beg + end - pos)); switch (str.readUnsignedByte(++end)) { case 'r': codePoint = '\r'; break; case 'n': codePoint = '\n'; break; case '\\': codePoint = '\\'; break; case '/': codePoint = '/'; break; case '"': codePoint = '\"'; break; case 'f': codePoint = '\f'; break; case 'b': codePoint = '\b'; break; case 't': codePoint = '\t'; break; case 'u': { codePoint = hexToDigit(str, ++end); end += 3; break; } default: throw new AssertionError("this should never happen"); } buf.append(codePoint); beg = (int) ((++end) - pos); } else { end++; } } buf.append(str, beg, (int) (beg + end - pos)); }
Example 16
Source File: Ed25519Test.java From Chronicle-Salt with Apache License 2.0 | 4 votes |
@Test public void signAndVerify() { final String SIGN_PRIVATE = "b18e1d0045995ec3d010c387ccfeb984d783af8fbb0f40fa7db126d889f6dadd"; Bytes publicKey = bytesWithZeros(32); Bytes secretKey = bytesWithZeros(64); Bytes privateKey = fromHex(SIGN_PRIVATE); Ed25519.privateToPublicAndSecret(publicKey, secretKey, privateKey); assertEquals(32, publicKey.readRemaining()); assertEquals(64, secretKey.readRemaining()); Bytes emptyMsg = bytesWithZeros(0); Bytes sigAndMsg0 = bytesWithZeros(ED25519_SECRETKEY_BYTES); Bytes sigAndMsg = bytesWithZeros(ED25519_SECRETKEY_BYTES); Ed25519.sign(sigAndMsg0, emptyMsg, secretKey); assertEquals(0, SODIUM.crypto_sign_ed25519(sigAndMsg.addressForWrite(0), new LongLongByReference(0), emptyMsg.addressForRead(0), 0, secretKey.addressForRead(0))); sigAndMsg.readPositionRemaining(0, 64); System.out.println(publicKey.toHexString()); System.out.println(privateKey.toHexString()); System.out.println(secretKey.toHexString()); assertEquals(sigAndMsg.toHexString(), sigAndMsg0.toHexString()); checkZeros(emptyMsg); checkZeros(secretKey); checkZeros(sigAndMsg); Bytes buffer = bytesWithZeros(ED25519_SECRETKEY_BYTES); assertTrue(Ed25519.verify(sigAndMsg, publicKey)); for (int i = 0; i < sigAndMsg.readRemaining(); i++) { byte old = sigAndMsg.readByte(i); sigAndMsg.writeByte(i, old ^ 1); assertFalse(Ed25519.verify(sigAndMsg, publicKey)); sigAndMsg.writeByte(i, old); } assertEquals(0, SODIUM.crypto_sign_ed25519_open(buffer.addressForWrite(0), new LongLongByReference(0), sigAndMsg.addressForRead(0), 0 + 64, publicKey.addressForRead(0))); sigAndMsg.readPositionRemaining(0, 64); System.out.println(sigAndMsg.toHexString()); }
Example 17
Source File: BytesMarshallableReaderWriter.java From Chronicle-Map with Apache License 2.0 | 4 votes |
@Override public long size(@NotNull V toWrite) { Bytes<?> bytes = Wires.acquireBytes(); toWrite.writeMarshallable(bytes); return bytes.readRemaining(); }