ppkarwasz commented on code in PR #781: URL: https://github.com/apache/commons-io/pull/781#discussion_r2328364931
########## src/main/java/org/apache/commons/io/FileSystem.java: ########## @@ -530,4 +623,76 @@ CharSequence trimExtension(final CharSequence cs) { final int index = indexOf(cs, '.', 0); return index < 0 ? cs : cs.subSequence(0, index); } + + private boolean isLegalFileLength(final CharSequence candidate, final Charset charset) { + if (candidate == null || candidate.length() == 0) { + return false; + } + if (lengthUnit == LengthUnit.CHARS) { + return candidate.length() <= getMaxFileNameLength(); + } + final CharsetEncoder encoder = charset.newEncoder(); + try { + final ByteBuffer buffer = encoder.encode(CharBuffer.wrap(candidate)); + return buffer.remaining() <= getMaxFileNameLength(); + } catch (CharacterCodingException e) { + // If we can't encode, it's not legal + return false; + } + } + + CharSequence truncateFileName(final CharSequence candidate, final Charset charset) { + final int maxFileNameLength = getMaxFileNameLength(); + // Character-based limit: simple substring if needed. + if (lengthUnit == LengthUnit.CHARS) { + return candidate.length() <= maxFileNameLength ? candidate : candidate.subSequence(0, maxFileNameLength); + } + + // Byte-based limit + return truncateByBytes(candidate, charset, maxFileNameLength); + } + + static CharSequence truncateByBytes(final CharSequence candidate, final Charset charset, final int maxBytes) { Review Comment: I moved this method to `LengthUnit` and renamed it `truncate`: https://github.com/apache/commons-io/pull/781/commits/9a1e9e41df93bf2e2c442637e9c5ba2c78d7dc72 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@commons.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org