// We presume 'data' is filled with the message, possibly over 510 // characters, to send, as a string. final String message = "lorem ipsum dolor sit amet..."; // This number will be 510 (the maximum length of an IRC message) // minus the prefix ("BotName!~user@host NOTICE OriginatorName :") final int safeLength = originator.safeNoticeLength(); // 1. Encode the message as UTF-8. This step may or may not be // necessary in some programming environments where all strings are // bytes. final byte[] data = asUTF8Bytes(message); int cur = 0; // 2. Walk along the message bytes... while (cur < data.length) { if (data.length - cur <= safeLength) { // End of message. Issue fully. originator.sendNotice(trim(asUTF8String(data, cur, data.length - cur))); cur = data.length; } else { final int first = cur; int last = cur; // Find the last space in this message. while (cur < first + safeLength) { if (data[cur] == (byte) ' ') last = cur + 1; cur++; } if (last == first) last = cur; // None found, this is a really long word... // Just cut the message at the safe length. else if (data[cur] == (byte) ' ') { // Spaces just after a word that would fit. last = cur + 1; while (last < data.length && data[last] == (byte) ' ') last++; } originator.sendNotice(trim(asUTF8String(data, first, last - first))); cur = last; } }