Java String Sanitize sanitizeIdentifier(String identifier)

Here you can find the source of sanitizeIdentifier(String identifier)

Description

Sanitizes a stream identifier

Stream identifiers are used to identify the content of a specific stream.

License

Open Source License

Parameter

Parameter Description
identifier Unfiltered identifier to sanitize

Return

the sanitized four-byte identifier

Declaration

public static String sanitizeIdentifier(String identifier) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from   w  ww  .j  av a 2s  .  c o  m*/
     * Sanitizes a stream identifier
     * <p>
     * Stream identifiers are used to identify the content of a specific stream.
     * It is stored in the stream header, just after the constant format identifier.
     * <p>
     * If the input identifier is longer than four bytes, it is trimmed to exactly four bytes.
     * If the input identifier is lesser than four bytes, white spaces are padded to the right up to four bytes.
     * <p>
     * The identifier must be composed only of US-ASCII letters and digits.
     * Any non-compliant character is ignored.  
     * 
     * @param identifier Unfiltered identifier to sanitize
     * @return the sanitized four-byte identifier
     */
    public static String sanitizeIdentifier(String identifier) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4 && i < identifier.length(); i++) {
            Character c = identifier.charAt(i);
            if (c.charValue() > 127 || !Character.isLetterOrDigit(c))
                continue;
            sb.append(c);
        }
        return String.format("%-4s", sb.toString());
    }
}

Related

  1. sanitizeForUri(String uri, String replace)
  2. sanitizeFullPrefixKey(String propKey)
  3. sanitizeGoogleId(String rawGoogleId)
  4. sanitizeHeader(String header)
  5. sanitizeID(String name)
  6. sanitizeIdentifierName(String input)
  7. sanitizeIDs(String str)
  8. sanitizeInput(String input)
  9. sanitizeInput(String string)