Java Code Examples for io.vertx.core.buffer.Buffer#appendBytes()
The following examples show how to use
io.vertx.core.buffer.Buffer#appendBytes() .
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: Helper.java From vertx-shell with Apache License 2.0 | 6 votes |
public static Buffer loadResource(String path) { URL resource = HttpTermServer.class.getResource(path); if (resource != null) { try { byte[] tmp = new byte[512]; InputStream in = resource.openStream(); Buffer buffer = Buffer.buffer(); while (true) { int l = in.read(tmp); if (l == -1) { break; } buffer.appendBytes(tmp, 0, l); } return buffer; } catch (IOException ignore) { } } return null; }
Example 2
Source File: UserHolder.java From vertx-web with Apache License 2.0 | 6 votes |
@Override public void writeToBuffer(Buffer buffer) { // try to get the user from the context otherwise fall back to any cached version final User user; synchronized (this) { user = context != null ? context.user() : this.user; // clear the context as this holder is not in a request anymore context = null; } if (user instanceof ClusterSerializable) { buffer.appendByte((byte)1); String className = user.getClass().getName(); if (className == null) { throw new IllegalStateException("Cannot serialize " + user.getClass().getName()); } byte[] bytes = className.getBytes(StandardCharsets.UTF_8); buffer.appendInt(bytes.length); buffer.appendBytes(bytes); ClusterSerializable cs = (ClusterSerializable)user; cs.writeToBuffer(buffer); } else { buffer.appendByte((byte)0); } }
Example 3
Source File: Utils.java From vertx-web with Apache License 2.0 | 6 votes |
public static Buffer readResourceToBuffer(String resource) { ClassLoader cl = getClassLoader(); try { Buffer buffer = Buffer.buffer(); try (InputStream in = cl.getResourceAsStream(resource)) { if (in == null) { return null; } int read; byte[] data = new byte[4096]; while ((read = in.read(data, 0, data.length)) != -1) { if (read == data.length) { buffer.appendBytes(data); } else { byte[] slice = new byte[read]; System.arraycopy(data, 0, slice, 0, slice.length); buffer.appendBytes(slice); } } } return buffer; } catch (IOException ioe) { throw new RuntimeException(ioe); } }
Example 4
Source File: SampleDatabaseWriter.java From vertx-in-action with MIT License | 5 votes |
public static void main(String[] args) { Vertx vertx = Vertx.vertx(); AsyncFile file = vertx.fileSystem().openBlocking("sample.db", new OpenOptions().setWrite(true).setCreate(true)); Buffer buffer = Buffer.buffer(); // Magic number buffer.appendBytes(new byte[] { 1, 2, 3, 4}); // Version buffer.appendInt(2); // DB name buffer.appendString("Sample database\n"); // Entry 1 String key = "abc"; String value = "123456-abcdef"; buffer .appendInt(key.length()) .appendString(key) .appendInt(value.length()) .appendString(value); // Entry 2 key = "foo@bar"; value = "Foo Bar Baz"; buffer .appendInt(key.length()) .appendString(key) .appendInt(value.length()) .appendString(value); file.end(buffer, ar -> vertx.close()); }
Example 5
Source File: SerializationSupport.java From vertx-vaadin with MIT License | 5 votes |
public static void writeToBuffer(Buffer buffer, Object object) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (ObjectOutputStream objectStream = new ObjectOutputStream(baos)) { objectStream.writeObject(object); objectStream.flush(); } catch (Exception ex) { logger.error("Error serializing object of type {}", object.getClass(), ex); } buffer.appendInt(baos.size()); buffer.appendBytes(baos.toByteArray()); }
Example 6
Source File: SerializationSupport.java From vertx-vaadin with MIT License | 5 votes |
public static void writeToBuffer(Buffer buffer, Object object) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (ObjectOutputStream objectStream = new ObjectOutputStream(baos)) { objectStream.writeObject(object); objectStream.flush(); } catch (Exception ex) { logger.error("Error serializing object of type {}", object.getClass(), ex); } buffer.appendInt(baos.size()); buffer.appendBytes(baos.toByteArray()); }
Example 7
Source File: EventMessageCodec.java From Summer with MIT License | 5 votes |
@Override public void encodeToWire(Buffer buffer, EventMessage eventMessage) { try { ByteArrayOutputStream b = new ByteArrayOutputStream(); ObjectOutputStream ob = new ObjectOutputStream(b); ob.writeObject(eventMessage); buffer.appendBytes(b.toByteArray()); }catch (Exception e){ } }
Example 8
Source File: JacksonCodec.java From kyoko with MIT License | 5 votes |
@Override public void encodeToWire(final Buffer buffer, final T object) { try { final byte[] data = JsonUtil.toJSON(object).getBytes(StandardCharsets.UTF_8); buffer.appendInt(data.length); buffer.appendBytes(data); } catch (IOException e) { throw new IllegalStateException(e); } }
Example 9
Source File: RequestImpl.java From vertx-redis-client with Apache License 2.0 | 5 votes |
Buffer encode(Buffer buffer) { buffer // array header .appendByte((byte) '*') .appendBytes(numToBytes(args.size() + 1)) .appendBytes(EOL) // command .appendBytes(cmd.getBytes()); for (final byte[] arg : args) { if (arg == null) { buffer.appendBytes(NULL_BULK); continue; } if (arg.length == 0) { buffer.appendBytes(EMPTY_BULK); continue; } buffer .appendByte((byte) '$') .appendBytes(numToBytes(arg.length)) .appendBytes(EOL) .appendBytes(arg) .appendBytes(EOL); } return buffer; }
Example 10
Source File: ParameterDecoder.java From vert.x-microservice with Apache License 2.0 | 5 votes |
@Override public void encodeToWire(Buffer buffer, Parameter parameter) { try { buffer.appendBytes(Serializer.serialize(parameter)); } catch (IOException e) { e.printStackTrace(); } }
Example 11
Source File: ServiceInfoDecoder.java From vert.x-microservice with Apache License 2.0 | 5 votes |
@Override public void encodeToWire(Buffer buffer, ServiceInfo serviceInfo) { try { buffer.appendBytes(Serializer.serialize(serviceInfo)); } catch (IOException e) { e.printStackTrace(); } }
Example 12
Source File: WriteHandler.java From nassh-relay with GNU General Public License v2.0 | 5 votes |
@Override public void handle(final RoutingContext context) { final HttpServerRequest request = context.request(); final HttpServerResponse response = context.response(); response.putHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0"); response.putHeader("Pragma", "no-cache"); if (request.params().contains("sid") && request.params().contains("wcnt") && request.params().contains("data")) { final UUID sid = UUID.fromString(request.params().get("sid")); final byte[] data = Base64.getUrlDecoder().decode(request.params().get("data")); response.setStatusCode(200); final LocalMap<String, Session> map = vertx.sharedData().getLocalMap(Constants.SESSIONS); final Session session = map.get(sid.toString()); if (session == null) { response.setStatusCode(410); response.end(); return; } session.setWrite_count(Integer.parseInt(request.params().get("wcnt"))); final Buffer message = Buffer.buffer(); message.appendBytes(data); vertx.eventBus().publish(session.getHandler(), message); response.end(); } else { response.setStatusCode(410); response.end(); } }
Example 13
Source File: ServiceExceptionMessageCodec.java From vertx-service-proxy with Apache License 2.0 | 5 votes |
@Override public void encodeToWire(Buffer buffer, ServiceException body) { buffer.appendInt(body.failureCode()); if (body.getMessage() == null) { buffer.appendByte((byte)0); } else { buffer.appendByte((byte)1); byte[] encoded = body.getMessage().getBytes(CharsetUtil.UTF_8); buffer.appendInt(encoded.length); buffer.appendBytes(encoded); } body.getDebugInfo().writeToBuffer(buffer); }
Example 14
Source File: ArrayWrappingBytes.java From incubator-tuweni with Apache License 2.0 | 4 votes |
@Override public void appendTo(Buffer buffer) { buffer.appendBytes(bytes, offset, length); }
Example 15
Source File: ArrayWrappingBytes.java From cava with Apache License 2.0 | 4 votes |
@Override public void appendTo(Buffer buffer) { buffer.appendBytes(bytes, offset, length); }
Example 16
Source File: ClientHandler.java From shadowsocks-vertx with Apache License 2.0 | 4 votes |
private boolean handleStageAddress() { int bufferLength = mPlainTextBufferQ.length(); String addr = null; // Construct the remote header. Buffer remoteHeader = Buffer.buffer(); int addrType = mPlainTextBufferQ.getByte(0); remoteHeader.appendByte((byte)(addrType)); if (addrType == ADDR_TYPE_IPV4) { // addr type (1) + ipv4(4) + port(2) if (bufferLength < 7) return false; try{ addr = InetAddress.getByAddress(mPlainTextBufferQ.getBytes(1, 5)).toString(); }catch(UnknownHostException e){ log.error("UnknownHostException:" + e.toString()); return true; } remoteHeader.appendBytes(mPlainTextBufferQ.getBytes(1,5)); compactPlainTextBufferQ(5); }else if (addrType == ADDR_TYPE_HOST) { short hostLength = mPlainTextBufferQ.getUnsignedByte(1); // addr type(1) + len(1) + host + port(2) if (bufferLength < hostLength + 4) return false; addr = mPlainTextBufferQ.getString(2, hostLength + 2); remoteHeader.appendByte((byte)hostLength).appendString(addr); compactPlainTextBufferQ(hostLength + 2); }else { log.warn("Unsupport addr type " + addrType); return true; } int port = mPlainTextBufferQ.getUnsignedShort(0); remoteHeader.appendShort((short)port); compactPlainTextBufferQ(2); log.info("Connecting to " + addr + ":" + port); connectToRemote(mConfig.server, mConfig.serverPort, remoteHeader); nextStage(); return false; }