docs: private static void addAttachments(Map<String, ? extends Attachment> attachments,
                                     Map<String, Object> outMap,
                                     boolean shouldInline,
                                     int minRevPos) {
          // Use a LinkedHashMap which has "predictable iteration order" - this means that the
          // serializer will output the _attachments entries in the same order as the multipart bodies
          // Note that we have already sorted entries by their key (see below where a TreeMap is used)
          // se we only need *this* map to have an iteration order which is in the insertion order
          LinkedHashMap<String, Object> attsMap = new LinkedHashMap<String, Object>();
          outMap.put("_attachments", attsMap);
  
          // first sort by attachment name - this means that the
          // entries in the _attachments dictionary are in the same
          // order as the multiparts
          attachments = new TreeMap<String, Attachment>(attachments);
          for (Map.Entry<String, ? extends Attachment> att : attachments.entrySet()) {
              // we need to cast down to SavedAttachment, which we know is what the AttachmentManager gives us
              SavedAttachment savedAtt = (SavedAttachment) att.getValue();
              HashMap<String, Object> theAtt = new HashMap<String, Object>();
              try {
                  // add the attachment if it's newer than the minimum revpos
                  if (savedAtt.revpos > minRevPos) {
                      if (!shouldInline) {
                          theAtt.put("follows", true);
                      } else {
                          theAtt.put("follows", false);
                          // base64 encode this attachment
                          ByteArrayOutputStream baos = new ByteArrayOutputStream();
                          OutputStream bos = Base64OutputStreamFactory.get(baos);
                          InputStream fis = savedAtt.getInputStream();
                          try {
                              int bufSiz = 1024;
                              byte[] buf = new byte[bufSiz];
                              int n = 0;
                              do {
                                  n = fis.read(buf);
                                  if (n > 0) {
                                      bos.write(buf, 0, n);
                                  }
                              } while (n > 0);
                              // out of paranoia, close and flush - docs don't say what you have to
                              // do to
  
                              // ensure all base64 is written, but close seems to do the right
                              // thing with
                              // the apache codec implementation
                              bos.flush();
                              bos.close();
                              theAtt.put("data", new String(baos.toByteArray(), "UTF-8")); //base64 of data
                          } finally {
                              IOUtils.closeQuietly(fis);
                          }
                      }
                      theAtt.put("length", savedAtt.length);
                      theAtt.put("encoded_length", savedAtt.encodedLength);
                      theAtt.put("content_type", savedAtt.type);
                      theAtt.put("revpos", savedAtt.revpos);
                      if (savedAtt.encoding != Attachment.Encoding.Plain) {
                          theAtt.put("encoding", savedAtt.encoding.toString().toLowerCase(Locale.ENGLISH));
                      }
                  } else {
                      // if the revpos of the attachment is higher than the minimum, it's a stub
                      theAtt.put("stub", true);
                  }
              } catch (IOException ioe) {
                  // if we can't read the file containing the attachment then skip it
                  // (this should only occur if someone tampered with the attachments directory
                  // or something went seriously wrong)
                  logger.log(Level.WARNING,String.format("Error reading attachment %s",att),ioe);
                  continue;
              }
              // now we are done, add the attachment to the map
              attsMap.put(att.getKey(), theAtt);
          }
      }