@Test public void testStripLineTo5Chars() { assertEquals( "a", LdifUtils.stripLineToNChars( "a", 5 ) ); assertEquals( "ab", LdifUtils.stripLineToNChars( "ab", 5 ) ); assertEquals( "abc", LdifUtils.stripLineToNChars( "abc", 5 ) ); assertEquals( "abcd", LdifUtils.stripLineToNChars( "abcd", 5 ) ); assertEquals( "abcde", LdifUtils.stripLineToNChars( "abcde", 5 ) ); assertEquals( "abcde\n f", LdifUtils.stripLineToNChars( "abcdef", 5 ) ); assertEquals( "abcde\n fg", LdifUtils.stripLineToNChars( "abcdefg", 5 ) ); assertEquals( "abcde\n fgh", LdifUtils.stripLineToNChars( "abcdefgh", 5 ) ); assertEquals( "abcde\n fghi", LdifUtils.stripLineToNChars( "abcdefghi", 5 ) ); assertEquals( "abcde\n fghi\n j", LdifUtils.stripLineToNChars( "abcdefghij", 5 ) ); assertEquals( "abcde\n fghi\n jk", LdifUtils.stripLineToNChars( "abcdefghijk", 5 ) ); assertEquals( "abcde\n fghi\n jkl", LdifUtils.stripLineToNChars( "abcdefghijkl", 5 ) ); assertEquals( "abcde\n fghi\n jklm", LdifUtils.stripLineToNChars( "abcdefghijklm", 5 ) ); assertEquals( "abcde\n fghi\n jklm\n n", LdifUtils.stripLineToNChars( "abcdefghijklmn", 5 ) ); assertEquals( "abcde\n fghi\n jklm\n no", LdifUtils.stripLineToNChars( "abcdefghijklmno", 5 ) ); assertEquals( "abcde\n fghi\n jklm\n nop", LdifUtils.stripLineToNChars( "abcdefghijklmnop", 5 ) ); }
public static String stripLineToNChars( String str, int nbChars ) { int strLength = str.length(); if ( strLength <= nbChars ) { return str; } if ( nbChars < 2 ) { throw new IllegalArgumentException( I18n.err( I18n.ERR_13474_LINE_LENGTH_TOO_SHORT ) ); } int charsPerLine = nbChars - 1; int remaining = ( strLength - nbChars ) % charsPerLine; int nbLines = 1 + ( ( strLength - nbChars ) / charsPerLine ) + ( remaining == 0 ? 0 : 1 ); int nbCharsTotal = strLength + nbLines + nbLines - 2; char[] buffer = new char[nbCharsTotal]; char[] orig = str.toCharArray(); int posSrc = 0; int posDst = 0; System.arraycopy( orig, posSrc, buffer, posDst, nbChars ); posSrc += nbChars; posDst += nbChars; for ( int i = 0; i < nbLines - 2; i++ ) { buffer[posDst++] = '\n'; buffer[posDst++] = ' '; System.arraycopy( orig, posSrc, buffer, posDst, charsPerLine ); posSrc += charsPerLine; posDst += charsPerLine; } buffer[posDst++] = '\n'; buffer[posDst++] = ' '; System.arraycopy( orig, posSrc, buffer, posDst, remaining == 0 ? charsPerLine : remaining ); return new String( buffer ); }
LdifUtils { public static String stripLineToNChars( String str, int nbChars ) { int strLength = str.length(); if ( strLength <= nbChars ) { return str; } if ( nbChars < 2 ) { throw new IllegalArgumentException( I18n.err( I18n.ERR_13474_LINE_LENGTH_TOO_SHORT ) ); } int charsPerLine = nbChars - 1; int remaining = ( strLength - nbChars ) % charsPerLine; int nbLines = 1 + ( ( strLength - nbChars ) / charsPerLine ) + ( remaining == 0 ? 0 : 1 ); int nbCharsTotal = strLength + nbLines + nbLines - 2; char[] buffer = new char[nbCharsTotal]; char[] orig = str.toCharArray(); int posSrc = 0; int posDst = 0; System.arraycopy( orig, posSrc, buffer, posDst, nbChars ); posSrc += nbChars; posDst += nbChars; for ( int i = 0; i < nbLines - 2; i++ ) { buffer[posDst++] = '\n'; buffer[posDst++] = ' '; System.arraycopy( orig, posSrc, buffer, posDst, charsPerLine ); posSrc += charsPerLine; posDst += charsPerLine; } buffer[posDst++] = '\n'; buffer[posDst++] = ' '; System.arraycopy( orig, posSrc, buffer, posDst, remaining == 0 ? charsPerLine : remaining ); return new String( buffer ); } }
LdifUtils { public static String stripLineToNChars( String str, int nbChars ) { int strLength = str.length(); if ( strLength <= nbChars ) { return str; } if ( nbChars < 2 ) { throw new IllegalArgumentException( I18n.err( I18n.ERR_13474_LINE_LENGTH_TOO_SHORT ) ); } int charsPerLine = nbChars - 1; int remaining = ( strLength - nbChars ) % charsPerLine; int nbLines = 1 + ( ( strLength - nbChars ) / charsPerLine ) + ( remaining == 0 ? 0 : 1 ); int nbCharsTotal = strLength + nbLines + nbLines - 2; char[] buffer = new char[nbCharsTotal]; char[] orig = str.toCharArray(); int posSrc = 0; int posDst = 0; System.arraycopy( orig, posSrc, buffer, posDst, nbChars ); posSrc += nbChars; posDst += nbChars; for ( int i = 0; i < nbLines - 2; i++ ) { buffer[posDst++] = '\n'; buffer[posDst++] = ' '; System.arraycopy( orig, posSrc, buffer, posDst, charsPerLine ); posSrc += charsPerLine; posDst += charsPerLine; } buffer[posDst++] = '\n'; buffer[posDst++] = ' '; System.arraycopy( orig, posSrc, buffer, posDst, remaining == 0 ? charsPerLine : remaining ); return new String( buffer ); } private  LdifUtils(); }
LdifUtils { public static String stripLineToNChars( String str, int nbChars ) { int strLength = str.length(); if ( strLength <= nbChars ) { return str; } if ( nbChars < 2 ) { throw new IllegalArgumentException( I18n.err( I18n.ERR_13474_LINE_LENGTH_TOO_SHORT ) ); } int charsPerLine = nbChars - 1; int remaining = ( strLength - nbChars ) % charsPerLine; int nbLines = 1 + ( ( strLength - nbChars ) / charsPerLine ) + ( remaining == 0 ? 0 : 1 ); int nbCharsTotal = strLength + nbLines + nbLines - 2; char[] buffer = new char[nbCharsTotal]; char[] orig = str.toCharArray(); int posSrc = 0; int posDst = 0; System.arraycopy( orig, posSrc, buffer, posDst, nbChars ); posSrc += nbChars; posDst += nbChars; for ( int i = 0; i < nbLines - 2; i++ ) { buffer[posDst++] = '\n'; buffer[posDst++] = ' '; System.arraycopy( orig, posSrc, buffer, posDst, charsPerLine ); posSrc += charsPerLine; posDst += charsPerLine; } buffer[posDst++] = '\n'; buffer[posDst++] = ' '; System.arraycopy( orig, posSrc, buffer, posDst, remaining == 0 ? charsPerLine : remaining ); return new String( buffer ); } private  LdifUtils(); static boolean isLDIFSafe( String str ); static String convertToLdif( Attributes attrs ); static String convertToLdif( Attributes attrs, int length ); static String convertToLdif( Attributes attrs, Dn dn, int length ); static String convertToLdif( Attributes attrs, Dn dn ); static String convertToLdif( Entry entry ); static String convertToLdif( Entry entry, boolean includeVersionInfo ); static String convertAttributesToLdif( Entry entry ); static Attributes getJndiAttributesFromLdif( String ldif ); static String convertToLdif( Entry entry, int length ); static String convertAttributesToLdif( Entry entry, int length ); static String convertToLdif( LdifEntry entry ); static String convertToLdif( LdifEntry entry, int length ); static String convertToLdif( Attribute attr ); static String convertToLdif( Attribute attr, int length ); static String stripLineToNChars( String str, int nbChars ); static Attributes createJndiAttributes( Object... avas ); }
LdifUtils { public static String stripLineToNChars( String str, int nbChars ) { int strLength = str.length(); if ( strLength <= nbChars ) { return str; } if ( nbChars < 2 ) { throw new IllegalArgumentException( I18n.err( I18n.ERR_13474_LINE_LENGTH_TOO_SHORT ) ); } int charsPerLine = nbChars - 1; int remaining = ( strLength - nbChars ) % charsPerLine; int nbLines = 1 + ( ( strLength - nbChars ) / charsPerLine ) + ( remaining == 0 ? 0 : 1 ); int nbCharsTotal = strLength + nbLines + nbLines - 2; char[] buffer = new char[nbCharsTotal]; char[] orig = str.toCharArray(); int posSrc = 0; int posDst = 0; System.arraycopy( orig, posSrc, buffer, posDst, nbChars ); posSrc += nbChars; posDst += nbChars; for ( int i = 0; i < nbLines - 2; i++ ) { buffer[posDst++] = '\n'; buffer[posDst++] = ' '; System.arraycopy( orig, posSrc, buffer, posDst, charsPerLine ); posSrc += charsPerLine; posDst += charsPerLine; } buffer[posDst++] = '\n'; buffer[posDst++] = ' '; System.arraycopy( orig, posSrc, buffer, posDst, remaining == 0 ? charsPerLine : remaining ); return new String( buffer ); } private  LdifUtils(); static boolean isLDIFSafe( String str ); static String convertToLdif( Attributes attrs ); static String convertToLdif( Attributes attrs, int length ); static String convertToLdif( Attributes attrs, Dn dn, int length ); static String convertToLdif( Attributes attrs, Dn dn ); static String convertToLdif( Entry entry ); static String convertToLdif( Entry entry, boolean includeVersionInfo ); static String convertAttributesToLdif( Entry entry ); static Attributes getJndiAttributesFromLdif( String ldif ); static String convertToLdif( Entry entry, int length ); static String convertAttributesToLdif( Entry entry, int length ); static String convertToLdif( LdifEntry entry ); static String convertToLdif( LdifEntry entry, int length ); static String convertToLdif( Attribute attr ); static String convertToLdif( Attribute attr, int length ); static String stripLineToNChars( String str, int nbChars ); static Attributes createJndiAttributes( Object... avas );  }