Java String Sanitize sanitize(final String singleOctets, byte[] dest)

Here you can find the source of sanitize(final String singleOctets, byte[] dest)

Description

Transforms the given string into the given destination byte array truncating each character into a byte and skipping carriage returns and line feeds if any.

License

Apache License

Parameter

Parameter Description
singleOctets non-null string containing only single octet characters
dest destination byte array

Exception

Parameter Description
IllegalArgumentException if the input string contains any multi-octet character

Return

the actual length of the destination byte array holding data

Declaration

static int sanitize(final String singleOctets, byte[] dest) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from w w w.j  a  v a2  s .c om*/
     * Transforms the given string into the given destination byte array
     * truncating each character into a byte and skipping carriage returns and
     * line feeds if any.
     * <p>
     * dmurray: "It so happens that we're currently only calling this method
     * with src.length == dest.length, in which case it works, but we could
     * theoretically get away with passing a smaller dest if we knew ahead of
     * time that src contained some number of spaces. In that case it looks like
     * this implementation would truncate the result."
     * <p>
     * hchar:
     * "Yes, but the truncation is the intentional behavior of this internal 
     * routine in that case."
     * 
     * @param singleOctets
     *            non-null string containing only single octet characters
     * @param dest
     *            destination byte array
     * 
     * @return the actual length of the destination byte array holding data
     * @throws IllegalArgumentException
     *             if the input string contains any multi-octet character
     */
    static int sanitize(final String singleOctets, byte[] dest) {
        final int capacity = dest.length;
        final char[] src = singleOctets.toCharArray();
        int limit = 0;

        for (int i = 0; i < capacity; i++) {
            final char c = src[i];

            if (c == '\r' || c == '\n' || c == ' ')
                continue;
            if (c > Byte.MAX_VALUE)
                throw new IllegalArgumentException(
                        "Invalid character found at position " + i + " for " + singleOctets);
            dest[limit++] = (byte) c;
        }
        return limit;
    }
}

Related

  1. sanitize(CharSequence string)
  2. sanitize(double[] outdata)
  3. sanitize(final String main)
  4. sanitize(final String name)
  5. sanitize(final String s)
  6. sanitize(String input)
  7. sanitize(String input)
  8. sanitize(String input, String prohibitedStringsRegexp)
  9. sanitize(String methodHeader)