@Test public void testStripLineToNChars() { String line = "abc"; try { LdifUtils.stripLineToNChars( line, 1 ); fail(); } catch ( IllegalArgumentException iae ) { } String res = LdifUtils.stripLineToNChars( line, 2 ); assertEquals( "ab\n c", res ); assertEquals( "abc", LdifUtils.stripLineToNChars( line, 3 ) ); }
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 );  }