* formatted.
*/
protected static String readNextString(ByteBuffer buffer) {
- int length = buffer.getInt();
- if (length < 0) {
+ int nbBytes = buffer.getInt();
+ if (nbBytes < 0) {
/* The string length should be positive */
return null;
}
- if (length == 0) {
+ if (nbBytes == 0) {
/* The string is explicitly an empty string */
return "";
}
- byte[] stringBytes = new byte[length];
+ byte[] stringBytes = new byte[nbBytes];
buffer.get(stringBytes);
return new String(stringBytes, SESSIOND_PROTOCOL_CHARSET).trim();
}
@Override
public LttngAgentResponse execute(ILttngTcpClientListener agent) {
final Collection<String> loggerList = agent.listAvailableEvents();
- int dataSize = 0;
-
- for (String event : agent.listAvailableEvents()) {
- dataSize += event.length() + 1;
- }
-
- return new SessiondListLoggersResponse(loggerList, dataSize);
+ return new SessiondListLoggersResponse(loggerList);
}
private static class SessiondListLoggersResponse extends LttngAgentResponse {
private final static int SIZE = 12;
private final Collection<String> loggers;
- private final int dataSize;
- public SessiondListLoggersResponse(Collection<String> loggers, int dataSize) {
+ public SessiondListLoggersResponse(Collection<String> loggers) {
this.loggers = loggers;
- this.dataSize = dataSize;
}
@Override
@Override
public byte[] getBytes() {
+ /*
+ * Compute the data size, which is the number of bytes of each
+ * encoded string, +1 per string for the \0
+ */
+ int dataSize = 0;
+ for (String logger : loggers) {
+ dataSize += logger.getBytes(SESSIOND_PROTOCOL_CHARSET).length + 1;
+ }
+
+ /* Prepare the buffer */
byte data[] = new byte[SIZE + dataSize];
ByteBuffer buf = ByteBuffer.wrap(data);
buf.order(ByteOrder.BIG_ENDIAN);
- /* Returned code */
+ /* Write the header section of the response */
buf.putInt(getReturnCode().getCode());
buf.putInt(dataSize);
buf.putInt(loggers.size());
+ /* Write the payload */
for (String logger : loggers) {
buf.put(logger.getBytes(SESSIOND_PROTOCOL_CHARSET));
/* NULL terminated byte after the logger name. */