@Override public void serialize(ByteBuffer bbuf, JsonGenerator gen, SerializerProvider provider) throws IOException { // first, simple case when wrapping an array... if (bbuf.hasArray()) { gen.writeBinary(bbuf.array(), bbuf.arrayOffset(), bbuf.limit()); return; } // the other case is more complicated however. Best to handle with InputStream wrapper. // But should we rewind it; and/or make a copy? ByteBuffer copy = bbuf.asReadOnlyBuffer(); if (copy.position() > 0) { copy.rewind(); } InputStream in = new ByteBufferBackedInputStream(copy); gen.writeBinary(in, copy.remaining()); in.close(); }
@SneakyThrows private OUT deserialize(InvokeResponse response) { return reader.readValue(new ByteBufferBackedInputStream(response.payload())); } }
/** Deserialize a JSON-RPC response from a ByteBuffer from a Lambda response payload. */ private Object readResponse(Type returnType, InvokeResponse response) { try { log.trace( "Raw response payload: " + StandardCharsets.UTF_8.decode(response.payload()).toString()); return streamClient.readResponse( returnType, new ByteBufferBackedInputStream(response.payload())); } catch (Throwable t) { if (t instanceof IOException) { log.warn("Could not read JSON-RPC response: ", t); } else { // This exception is raised when deserialized from the remote call, and must be // handled/logged in calling code; so only log at DEBUG level log.debug("Read an exception value from JSON-RPC response: ", t); } // wrap the raw Throwable from the JSON RPC client so that this method can be used in // CompletableFuture::thenApply throw new CompletionException(t); } } }
ByteBufferBackedInputStream input = new ByteBufferBackedInputStream( messageData );
@Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { FullHttpRequest request = (FullHttpRequest) msg; Document document = documentBuilder.parse(new ByteBufferBackedInputStream(request.content().nioBuffer())); NodeList nodes = (NodeList) messageExpression.evaluate(document, XPathConstants.NODESET); List<Position> positions = new LinkedList<>(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, xPath.evaluate("esnName", node)); if (deviceSession != null) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setValid(true); position.setTime(DateUtil.parseDate(xPath.evaluate("timestamp", node))); position.setLatitude(Double.parseDouble(xPath.evaluate("latitude", node))); position.setLongitude(Double.parseDouble(xPath.evaluate("longitude", node))); position.set(Position.KEY_EVENT, xPath.evaluate("messageType", node)); positions.add(position); } } sendResponse(channel, HttpResponseStatus.OK); return positions; }
@Override @SneakyThrows public CompletableFuture<InvokeResponse> invoke(final InvokeRequest invokeRequest) { ByteBufferBackedInputStream inputStream = new ByteBufferBackedInputStream(invokeRequest.payload()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); handler.handleRequest(inputStream, outputStream, null); return CompletableFuture.completedFuture( InvokeResponse.builder().payload(ByteBuffer.wrap(outputStream.toByteArray())).build()); } }
@Test public void serializesInputsUsingMapper() throws Exception { TestOutput output = function.call(new TestInput("test")); verify(client).invoke(requestArgument.capture()); ByteBuffer actualPayload = requestArgument.getValue().payload(); assertThat( mapper .readValue(new ByteBufferBackedInputStream(actualPayload), TestInput.class) .getInput(), equalTo("test")); }
@Override public boolean willDecode(ByteBuffer bytes) { try { mapper.readTree(new ByteBufferBackedInputStream(bytes)); } catch (IOException e) { logger.warn("invalidate json", e); return false; } return true; } }
@Override public <U> U deserialize(BinaryData rawPayload, Class<U> targetType) { try { return objectMapper.readValue(new ByteBufferBackedInputStream(rawPayload.toByteBuffer()), targetType); } catch (IOException e) { throw new IllegalArgumentException("Error deserializing payload", e); } } }
@Override public T decode(ByteBuffer bytes) throws DecodeException { try { return mapper.readValue(new ByteBufferBackedInputStream(bytes), objectClass); } catch (IOException e) { throw new DecodeException(bytes, "decode json error", e); } }
@Override public void serialize(ByteBuffer bbuf, JsonGenerator gen, SerializerProvider provider) throws IOException { // first, simple case when wrapping an array... if (bbuf.hasArray()) { gen.writeBinary(bbuf.array(), bbuf.arrayOffset(), bbuf.limit()); return; } // the other case is more complicated however. Best to handle with InputStream wrapper. // But should we rewind it; and/or make a copy? ByteBuffer copy = bbuf.asReadOnlyBuffer(); if (copy.position() > 0) { copy.rewind(); } InputStream in = new ByteBufferBackedInputStream(copy); gen.writeBinary(in, copy.remaining()); in.close(); }
@Override public void serialize(ByteBuffer bbuf, JsonGenerator gen, SerializerProvider provider) throws IOException { // first, simple case when wrapping an array... if (bbuf.hasArray()) { gen.writeBinary(bbuf.array(), 0, bbuf.limit()); return; } // the other case is more complicated however. Best to handle with InputStream wrapper. // But should we rewind it; and/or make a copy? ByteBuffer copy = bbuf.asReadOnlyBuffer(); if (copy.position() > 0) { copy.rewind(); } InputStream in = new ByteBufferBackedInputStream(copy); gen.writeBinary(in, copy.remaining()); in.close(); } }
@Override public List<CSVReportEntry> apply(final GetCredentialReportResult report) { Assert.state(Textcsv.toString().equals(report.getReportFormat()), "unknown credential report format: " + report.getReportFormat()); try (final Reader r = new BufferedReader(new InputStreamReader(new ByteBufferBackedInputStream(report.getContent())))) { final CSVParser parser = new CSVParser(r, CSV_FORMAT); final Map<String, Integer> headers = parser.getHeaderMap(); Assert.state(headers.containsKey("user"), "Header 'user' not found in CSV"); Assert.state(headers.containsKey("arn"), "Header 'arn' not found in CSV"); Assert.state(headers.containsKey("password_enabled"), "Header 'password_enabled' not found in CSV"); Assert.state(headers.containsKey("mfa_active"), "Header 'mfa_active' not found in CSV"); Assert.state(headers.containsKey("access_key_1_active"), "Header 'access_key_1_active' not found in CSV"); Assert.state(headers.containsKey("access_key_2_active"), "Header 'access_key_2_active' not found in CSV"); return stream(parser.spliterator(), false).map(this::toCSVReportEntry).filter(Objects::nonNull).collect(toList()); } catch (final IOException e) { throw new RuntimeException("Could not read csv report", e); } }
@Override public void serialize(ByteBuffer bbuf, JsonGenerator gen, SerializerProvider provider) throws IOException { // first, simple case when wrapping an array... if (bbuf.hasArray()) { gen.writeBinary(bbuf.array(), 0, bbuf.limit()); return; } // the other case is more complicated however. Best to handle with InputStream wrapper. // But should we rewind it; and/or make a copy? ByteBuffer copy = bbuf.asReadOnlyBuffer(); if (copy.position() > 0) { copy.rewind(); } InputStream in = new ByteBufferBackedInputStream(copy); gen.writeBinary(in, copy.remaining()); in.close(); } }
@Override public void serialize(ByteBuffer bbuf, JsonGenerator gen, SerializerProvider provider) throws IOException { // first, simple case when wrapping an array... if (bbuf.hasArray()) { gen.writeBinary(bbuf.array(), 0, bbuf.limit()); return; } // the other case is more complicated however. Best to handle with InputStream wrapper. // But should we rewind it; and/or make a copy? ByteBuffer copy = bbuf.asReadOnlyBuffer(); if (copy.position() > 0) { copy.rewind(); } InputStream in = new ByteBufferBackedInputStream(copy); gen.writeBinary(in, copy.remaining()); in.close(); } }
public InputStream getBodyAsInputStream() { return new ByteBufferBackedInputStream(getRecord().getData().duplicate()); }
@Override @SuppressWarnings("unchecked") public JoseHeader deserialize(BinaryData data) throws JwtMalformedTokenException { try { Map<String, Object> headerValues = objectMapper.readValue( new ByteBufferBackedInputStream(data.toByteBuffer()), Map.class); return new DefaultJoseHeader(headerValues); } catch (IOException e) { throw new JwtMalformedTokenException("Error deserializing JWS header", e); } } }
public void send(String dbAndTableTag, ByteBuffer dataBuffer) throws IOException { String[] dbAndTable = dbAndTableTag.split("\\."); // TODO: Validation String database = dbAndTable[0]; String table = dbAndTable[1]; File file = File.createTempFile("tmp-fluency-", ".msgpack.gz"); try { try (InputStream in = new ByteBufferBackedInputStream(dataBuffer); OutputStream out = new GZIPOutputStream( Files.newOutputStream( file.toPath(), StandardOpenOption.WRITE))) { copyStreams(in, out); } String uniqueId = UUID.randomUUID().toString(); Failsafe.with(retryPolicy).run(() -> importData(database, table, uniqueId, file)); } finally { if (!file.delete()) { LOG.warn("Failed to delete a temp file: {}", file.getAbsolutePath()); } } }
@Override public void accept(Event<KinesisReactorBridge.KinesisRecord> t) { try { KinesisRecord record = (KinesisRecord) t.getData(); JsonNode n = mapper.readTree(new ByteBufferBackedInputStream(record.getRecord().getData())); Event<JsonNode> event = Event.wrap(n); EventUtil.copyEventHeaders(t, event); record.getBridge().getEventBus().notify(n, event); } catch (IOException | RuntimeException e) { if (logger.isDebugEnabled()) { logger.debug("could not parse json", e); } else { logger.warn("could not parse json: " + e.getMessage()); } } }
message = serDe.deserialize(new ByteBufferBackedInputStream(rawData), messageClass);