/** Retrieves the character at the {@code index}. * * @param index the index of the character to retrieve. * @return the char value. * @throws IndexOutOfBoundsException if {@code index} is negative or greater than or equal to the current {@link #length()}. */ public char charAt (int index) { if (index < 0 || index >= length) { throw new StringIndexOutOfBoundsException(index); } return chars[index]; }
public void testToBytesBinaryTrailingBackslashes() throws Exception { try { Bytes.toBytesBinary("abc\\x00\\x01\\"); } catch (StringIndexOutOfBoundsException ex) { fail("Illegal string access: " + ex.getMessage()); } }
private String[] getRowSamples( int position, int length ) { if ( position < 0 || position + length < 0 ) { return new String[0]; } String[] retval = new String[rows.size()]; for ( int i = 0; i < rows.size(); i++ ) { String line = rows.get( i ); if ( position < line.length() ) { if ( position + length >= line.length() ) { retval[i] = line.substring( position ); } else { try { retval[i] = line.substring( position, position + length ); } catch ( StringIndexOutOfBoundsException e ) { System.out.println( "SIOOB: " + e.toString() ); } } } else { retval[i] = ""; } } return retval; }
e.printStackTrace(); continue;
protected String getCurrentVersion(URL url, String driverName) { String currentVersion = ""; try { currentVersion = url.getFile().substring( url.getFile().indexOf(SLASH) + 1, url.getFile().lastIndexOf(SLASH)); } catch (StringIndexOutOfBoundsException e) { log.trace("Exception getting version of URL {} ({})", url, e.getMessage()); } return currentVersion; }
/** * Decode the encoded bytes in value and write them to the OutputStream. */ private static void decodeBytes(String value, OutputStream os) throws ParseException, IOException { /* * Decode the ASCII characters in value * and write them to the stream. */ int i; for (i = 0; i < value.length(); i++) { char c = value.charAt(i); if (c == '%') { try { String hex = value.substring(i + 1, i + 3); c = (char)Integer.parseInt(hex, 16); i += 2; } catch (NumberFormatException ex) { if (decodeParametersStrict) throw new ParseException(ex.toString()); } catch (StringIndexOutOfBoundsException ex) { if (decodeParametersStrict) throw new ParseException(ex.toString()); } } os.write((byte)c); } } }
/** Sets the character at the {@code index}. * * @param index the zero-based index of the character to replace. * @param ch the character to set. * @throws IndexOutOfBoundsException if {@code index} is negative or greater than or equal to the current {@link #length()}. */ public void setCharAt (int index, char ch) { if (0 > index || index >= length) { throw new StringIndexOutOfBoundsException(index); } chars[index] = ch; }
map.put(SyslogHeaders.ERRORS, "Unexpected end of message: " + sob.getMessage()); map.put(SyslogHeaders.UNDECODED, line);
/** * Decode the encoded bytes in value and write them to the OutputStream. */ private static void decodeBytes(String value, OutputStream os) throws ParseException, IOException { /* * Decode the ASCII characters in value * and write them to the stream. */ int i; for (i = 0; i < value.length(); i++) { char c = value.charAt(i); if (c == '%') { try { String hex = value.substring(i + 1, i + 3); c = (char)Integer.parseInt(hex, 16); i += 2; } catch (NumberFormatException ex) { if (decodeParametersStrict) throw new ParseException(ex.toString()); } catch (StringIndexOutOfBoundsException ex) { if (decodeParametersStrict) throw new ParseException(ex.toString()); } } os.write((byte)c); } } }
/** Retrieves the character at the {@code index}. * * @param index the index of the character to retrieve. * @return the char value. * @throws IndexOutOfBoundsException if {@code index} is negative or greater than or equal to the current {@link #length()}. */ public char charAt (int index) { if (index < 0 || index >= length) { throw new StringIndexOutOfBoundsException(index); } return chars[index]; }
@Test public void testAppendCharBufferException() throws Exception { final TextStringBuilder sb = new TextStringBuilder("1234567890"); final String text = "Test"; final CharBuffer buffer = CharBuffer.allocate(sb.size() + text.length()); buffer.put(text); buffer.flip(); try { sb.append(buffer, -1, 12); } catch (final StringIndexOutOfBoundsException e) { assertEquals("startIndex must be valid", e.getMessage()); } try { sb.append(buffer, 0, -1); } catch (final StringIndexOutOfBoundsException e) { assertEquals("length must be valid", e.getMessage()); } sb.append(buffer); assertEquals("1234567890Test", sb.toString()); }
/** * A special wrapper for a StringBuffer that keeps track of the * number of characters used in a line, wrapping to a new line * as necessary; for use by the toString method. */ private static class ToStringBuffer { private int used; // keep track of how much used on current line private StringBuffer sb = new StringBuffer(); public ToStringBuffer(int used) { this.used = used; } public void addNV(String name, String value) { sb.append("; "); used += 2; int len = name.length() + value.length() + 1; if (used + len > 76) { // overflows ... sb.append("\r\n\t"); // .. start new continuation line used = 8; // account for the starting <tab> char } sb.append(name).append('='); used += name.length() + 1; if (used + value.length() > 76) { // still overflows ... // have to fold value String s = MimeUtility.fold(used, value); sb.append(s); int lastlf = s.lastIndexOf('\n'); if (lastlf >= 0) // always true used += s.length() - lastlf - 1;
/** Sets the character at the {@code index}. * * @param index the zero-based index of the character to replace. * @param ch the character to set. * @throws IndexOutOfBoundsException if {@code index} is negative or greater than or equal to the current {@link #length()}. */ public void setCharAt (int index, char ch) { if (0 > index || index >= length) { throw new StringIndexOutOfBoundsException(index); } chars[index] = ch; }
@Test public void testAppendCharBufferException() throws Exception { final StrBuilder sb = new StrBuilder("1234567890"); final String text = "Test"; final CharBuffer buffer = CharBuffer.allocate(sb.size() + text.length()); buffer.put(text); buffer.flip(); try { sb.append(buffer, -1, 12); } catch (final StringIndexOutOfBoundsException e) { assertEquals("startIndex must be valid", e.getMessage()); } try { sb.append(buffer, 0, -1); } catch (final StringIndexOutOfBoundsException e) { assertEquals("length must be valid", e.getMessage()); } sb.append(buffer); assertEquals("1234567890Test", sb.toString()); }
/** * A special wrapper for a StringBuffer that keeps track of the * number of characters used in a line, wrapping to a new line * as necessary; for use by the toString method. */ private static class ToStringBuffer { private int used; // keep track of how much used on current line private StringBuilder sb = new StringBuilder(); public ToStringBuffer(int used) { this.used = used; } public void addNV(String name, String value) { sb.append("; "); used += 2; int len = name.length() + value.length() + 1; if (used + len > 76) { // overflows ... sb.append("\r\n\t"); // .. start new continuation line used = 8; // account for the starting <tab> char } sb.append(name).append('='); used += name.length() + 1; if (used + value.length() > 76) { // still overflows ... // have to fold value String s = MimeUtility.fold(used, value); sb.append(s); int lastlf = s.lastIndexOf('\n'); if (lastlf >= 0) // always true used += s.length() - lastlf - 1;
/** * Validates parameters defining a single index in the builder. * * @param index the index, must be valid * @throws IndexOutOfBoundsException if the index is invalid */ protected void validateIndex(final int index) { if (index < 0 || index > size) { throw new StringIndexOutOfBoundsException(index); } }
public void testToBytesBinaryTrailingBackslashes() throws Exception { try { Bytes.toBytesBinary("abc\\x00\\x01\\"); } catch (StringIndexOutOfBoundsException ex) { fail("Illegal string access: " + ex.getMessage()); } }
} catch (StringIndexOutOfBoundsException ex) { if (decodeParametersStrict) throw new ParseException(ex.toString());
/** * Validates parameters defining a single index in the builder. * * @param index the index, must be valid * @throws IndexOutOfBoundsException if the index is invalid */ protected void validateIndex(int index) { if (index < 0 || index > size) { throw new StringIndexOutOfBoundsException(index); } }
@Test public void testIsAlphaNumericThrowsStringIndexOutOfBoundsException() throws Throwable { try { ISOUtil.isAlphaNumeric(" "); fail("Expected StringIndexOutOfBoundsException to be thrown"); } catch (StringIndexOutOfBoundsException ex) { assertEquals("ex.getMessage()", "String index out of range: 1", ex.getMessage()); } }