@Override public void append(ByteBuffer body, boolean immutable) throws IOException { if (immutable) { bodies.add(body); } else { bodies.add(ByteBuffers.copy(body)); } }
@Override public int write(ByteBuffer data) throws IOException { int size = data.remaining(); ByteBuffers.writeToStream(data, outputStream); return size; }
@Override public final void publish(String namespace, String topic, final Charset charset, Iterator<String> payloads) throws IOException, TopicNotFoundException { publish(namespace, topic, Iterators.transform(payloads, input -> ByteBuffers.getByteArray(charset.encode(input)))); }
void append(ILoggingEvent event) throws IOException { // If the event is already a LoggingEvent, we don't need to re-encode. if (event instanceof LoggingEvent) { ByteBuffer encoded = ((LoggingEvent) event).getEncoded(); if (encoded != null) { // TODO: CDAP-8735 Remove the copying after upgraded to Avro 1.7.4+ dataFileWriter.appendEncoded(ByteBuffers.copy(encoded)); } else { dataFileWriter.append(((LoggingEvent) event).getRecord()); } return; } dataFileWriter.append(serializer.toGenericRecord(event)); }
private void testWriteBuffer(ByteBuffer buffer, String content) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); // Write the whole buffer ByteBuffers.writeToStream(buffer, os); Assert.assertEquals(content, new String(os.toByteArray(), Charsets.UTF_8)); // Write part of the buffer int pos = content.length() / 2; buffer.position(pos); os.reset(); ByteBuffers.writeToStream(buffer, os); Assert.assertEquals(content.substring(pos), new String(os.toByteArray(), Charsets.UTF_8)); // Write with a slice of the buffer pos = content.length() / 3; buffer.position(pos); os.reset(); ByteBuffers.writeToStream(buffer.slice(), os); Assert.assertEquals(content.substring(pos), new String(os.toByteArray(), Charsets.UTF_8)); } }
@Override public final void publish(String namespace, String topic, final Charset charset, Iterator<String> payloads) throws IOException, TopicNotFoundException { publish(namespace, topic, Iterators.transform(payloads, input -> ByteBuffers.getByteArray(charset.encode(input)))); }
void append(ILoggingEvent event) throws IOException { // If the event is already a LoggingEvent, we don't need to re-encode. if (event instanceof LoggingEvent) { ByteBuffer encoded = ((LoggingEvent) event).getEncoded(); if (encoded != null) { // TODO: CDAP-8735 Remove the copying after upgraded to Avro 1.7.4+ dataFileWriter.appendEncoded(ByteBuffers.copy(encoded)); } else { dataFileWriter.append(((LoggingEvent) event).getRecord()); } return; } dataFileWriter.append(serializer.toGenericRecord(event)); }
/** * Creates a {@link CloseableIterator} of {@link RawMessage} based on the given fetch request. */ private CloseableIterator<RawMessage> fetchMessages(GenericRecord fetchRequest, TopicId topicId) throws IOException, TopicNotFoundException { MessageFetcher fetcher = messagingService.prepareFetch(topicId); Object startFrom = fetchRequest.get("startFrom"); if (startFrom != null) { if (startFrom instanceof ByteBuffer) { // start message id is specified fetcher.setStartMessage(Bytes.toBytes((ByteBuffer) startFrom), (Boolean) fetchRequest.get("inclusive")); } else if (startFrom instanceof Long) { // start by timestamp is specified fetcher.setStartTime((Long) startFrom); } else { // This shouldn't happen as it's guaranteed by the schema LOG.warn("Ignore unrecognized type for startFrom. Type={}, Value={}", startFrom.getClass(), startFrom); } } Integer limit = (Integer) fetchRequest.get("limit"); if (limit != null) { fetcher.setLimit(limit); } ByteBuffer encodedTx = (ByteBuffer) fetchRequest.get("transaction"); if (encodedTx != null) { fetcher.setTransaction(TRANSACTION_CODEC.decode(ByteBuffers.getByteArray(encodedTx))); } return fetcher.fetch(); }
/** * Creates a {@link CloseableIterator} of {@link RawMessage} based on the given fetch request. */ private CloseableIterator<RawMessage> fetchMessages(GenericRecord fetchRequest, TopicId topicId) throws IOException, TopicNotFoundException { MessageFetcher fetcher = messagingService.prepareFetch(topicId); Object startFrom = fetchRequest.get("startFrom"); if (startFrom != null) { if (startFrom instanceof ByteBuffer) { // start message id is specified fetcher.setStartMessage(Bytes.toBytes((ByteBuffer) startFrom), (Boolean) fetchRequest.get("inclusive")); } else if (startFrom instanceof Long) { // start by timestamp is specified fetcher.setStartTime((Long) startFrom); } else { // This shouldn't happen as it's guaranteed by the schema LOG.warn("Ignore unrecognized type for startFrom. Type={}, Value={}", startFrom.getClass(), startFrom); } } Integer limit = (Integer) fetchRequest.get("limit"); if (limit != null) { fetcher.setLimit(limit); } ByteBuffer encodedTx = (ByteBuffer) fetchRequest.get("transaction"); if (encodedTx != null) { fetcher.setTransaction(TRANSACTION_CODEC.decode(ByteBuffers.getByteArray(encodedTx))); } return fetcher.fetch(); }