- ByteBuffer buffer = ByteBuffer.allocate(totalArraySize);
- buffer.order(NATIVE_ORDER);
- buffer.clear();
-
- for (Map.Entry<String, Map<String, Integer>> entry : enabledContexts) {
- String requestedRetrieverName = entry.getKey();
- Map<String, Integer> requestedContexts = entry.getValue();
-
- IContextInfoRetriever retriever = contextManager.getContextInfoRetriever(requestedRetrieverName);
-
- for (String requestedContext : requestedContexts.keySet()) {
- Object contextInfo;
- if (retriever == null) {
- contextInfo = null;
- } else {
- contextInfo = retriever.retrieveContextInfo(requestedContext);
- /*
- * 'contextInfo' can still be null here, which would
- * indicate the retriever does not supply this context. We
- * will still write this information so that the tracer can
- * know about it.
- */
- }
+ /* Compute the total number of contexts (flatten the map) */
+ int totalArraySize = 0;
+ for (Map.Entry<String, Map<String, Integer>> contexts : enabledContexts) {
+ totalArraySize += contexts.getValue().size() * ENTRY_LENGTH;
+ }
+
+ /* Prepare the ByteBuffer that will generate the "entries" array */
+ ByteBuffer entriesBuffer = ByteBuffer.allocate(totalArraySize);
+ entriesBuffer.order(NATIVE_ORDER);
+ entriesBuffer.clear();
+
+ /* Prepare the streams that will generate the "strings" array */
+ ByteArrayOutputStream stringsBaos = new ByteArrayOutputStream();
+ DataOutputStream stringsDos = new DataOutputStream(stringsBaos);
+
+ try {
+ for (Map.Entry<String, Map<String, Integer>> entry : enabledContexts) {
+ String requestedRetrieverName = entry.getKey();
+ Map<String, Integer> requestedContexts = entry.getValue();
+
+ IContextInfoRetriever retriever = contextManager.getContextInfoRetriever(requestedRetrieverName);
+
+ for (String requestedContext : requestedContexts.keySet()) {
+ Object contextInfo;
+ if (retriever == null) {
+ contextInfo = null;
+ } else {
+ contextInfo = retriever.retrieveContextInfo(requestedContext);
+ /*
+ * 'contextInfo' can still be null here, which would
+ * indicate the retriever does not supply this context.
+ * We will still write this information so that the
+ * tracer can know about it.
+ */
+ }