- StringBuilder dataBuilder = new StringBuilder();
- if (data.getGroup() != null && !data.getGroup().trim().equals("")) {
- dataBuilder.append(data.getGroup().trim());
- dataBuilder.append('.');
- }
- dataBuilder.append(data.getName());
- for (TypeInfo type : data) {
- dataBuilder.append(';');
- dataBuilder.append(type.getName());
- if (type.getValue() != null
- && !type.getValue().trim().equals("")) {
- dataBuilder.append('=');
- dataBuilder.append(type.getValue());
- }
- }
- dataBuilder.append(':');
-
- // TODO: bkey!
- dataBuilder.append(data.getValue());
-
- // RFC says: Content lines SHOULD be folded to a maximum width of 75
- // octets -> since it is SHOULD, we will just cut it as 74/75 chars
- // depending if the last one fits in one char (note: chars != octet)
- boolean continuation = false;
- while (dataBuilder.length() > 0) {
- int stop = 74;
- if (continuation)
- stop--; // the space takes 1
- if (dataBuilder.length() > stop) {
- char car = dataBuilder.charAt(stop - 1);
- // RFC forbids cutting a character in 2
- if (Character.isHighSurrogate(car)) {
- stop++;
- }
- }
-
- stop = Math.min(stop, dataBuilder.length());
- if (continuation) {
- lines.add(' ' + dataBuilder.substring(0, stop));
- } else {
- lines.add(dataBuilder.substring(0, stop));
- }
- dataBuilder.delete(0, stop);
-
- continuation = true;
- }