Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2007-2008 Mozilla Foundation
   *
   * Permission is hereby granted, free of charge, to any person obtaining a 
   * copy of this software and associated documentation files (the "Software"), 
   * to deal in the Software without restriction, including without limitation 
   * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
   * and/or sell copies of the Software, and to permit persons to whom the 
   * Software is furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included in 
  * all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
  * DEALINGS IN THE SOFTWARE.
  */
 
 package org.whattf.io;
 
 
 
 public class DataUri {
 
     public static boolean startsWithData(String uri) {
         return uri != null && uri.length() >= 5
                 && (uri.charAt(0) == 'd' || uri.charAt(0) == 'D')
                 && (uri.charAt(1) == 'a' || uri.charAt(1) == 'A')
                 && (uri.charAt(2) == 't' || uri.charAt(2) == 'T')
                 && (uri.charAt(3) == 'a' || uri.charAt(3) == 'A')
                 && (uri.charAt(4) == ':');
     }
     
     private enum State {
         AT_START, IN_SUPERTYPE, AT_SUBTYPE_START, IN_SUBTYPE, SEMICOLON_SEEN, WS_BEFORE_SEMICOLON, IN_PARAM_NAME, EQUALS_SEEN, IN_QUOTED_STRING, IN_UNQUOTED_STRING, IN_QUOTED_PAIR, CLOSE_QUOTE_SEEN
     }
     
     private String contentType;
     
     private InputStream inputStream;
    
    

Throws:
java.io.IOException, MalformedURLException
 
     protected void init(IRI urithrows IOExceptionMalformedURLException {
         if (!uri.getScheme().equals("data")) {
             throw new IllegalArgumentException("The input did not start with data:.");
         }
 
         if (uri.getRawFragment() != null) {
             throw new MalformedURLException("Fragment is not allowed for data: URIs according to RFC 2397. But if strictly comply with RFC 3986, ignore this error.");
         }
 
         InputStream is = new PercentDecodingReaderInputStream(new StringReader(uri.getRawPath()));
         StringBuilder sb = new StringBuilder();
         State state = .;
         int i = 0; // string counter
         for (;;i++) {
             int b = is.read();
             if (b == -1) {
                 throw new MalformedURLException("Premature end of URI.");
             }
             if (b >= 0x80) {
                 throw new MalformedURLException("Non-ASCII character in MIME type part of the data URI.");                
             }
             char c = (charb;
             sb.append(c);
             switch (state) {
                 case :
                     if (isTokenChar(c)) {
                         state = .;
                         continue;
                     } else if (c == ';') {
                         sb.setLength(0);
                         sb.append("text/plain;");
                         state = .;
                         continue;
                     } else if (c == ',') {
                          = "text/plain;charset=US-ASCII";
                          = is;
                         return;
                     } else {
                         throw newDatatypeException(i
                                 "Expected a token character or a semicolon but saw ",
                                         c" instead.");
                     }
                 case :
                     if (isTokenChar(c)) {
                        continue;
                    } else if (c == '/') {
                        state = .;
                        continue;
                    } else {
                        throw newDatatypeException(i
                                "Expected a token character or \u201C/\u201D but saw ",
                                        c" instead.");
                    }
                case :
                    if (isTokenChar(c)) {
                        state = .;
                        continue;
                    } else {
                        throw newDatatypeException(i
                                "Expected a token character but saw ",
                                        c" instead.");
                    }
                case :
                    if (isTokenChar(c)) {
                        continue;
                    } else if (c == ';') {
                        state = .;
                        continue;
                    } else if (isWhitespace(c)) {
                        state = .;
                        continue;
                    } else if (c == ',') {
                         = sb.substring(0, sb.length() - 1);
                         = is;
                        return;
                    } else {
                        throw newDatatypeException(i
                                "Expected a token character, whitespace, a semicolon or a comma but saw ",
                                        c" instead.");
                    }
                case :
                    if (isWhitespace(c)) {
                        continue;
                    } else if (c == ';') {
                        state = .;
                        continue;
                    } else {
                        throw newDatatypeException(i
                                "Expected whitespace or a semicolon but saw ",
                                        c" instead.");
                    }
                case :
                    if (isWhitespace(c)) {
                        continue;
                    } else if (isTokenChar(c)) {
                        state = .;
                        continue;
                    } else {
                        throw newDatatypeException(i
                                "Expected whitespace or a token character but saw ",
                                        c" instead.");
                    }
                case :
                    if (isTokenChar(c)) {
                        continue;
                    } else if (c == '=') {
                        state = .;
                        continue;
                    } else if (c == ',') {
                        // let's see if we had ;base64,
                        int baseFirst = sb.length() - 8;
                        if (baseFirst >= 0 && ";base64,".equals(sb.substring(baseFirstsb.length()))) {
                             = sb.substring(0, baseFirst);
                             = new Base64InputStream(is);
                            return;
                        }
                    } else {
                        throw newDatatypeException(i
                                "Expected an equals sign, a comma or a token character but saw ",
                                        c" instead.");
                    }
                case :
                    if (c == '\"') {
                        state = .;
                        continue;
                    } else if (isTokenChar(c)) {
                        state = .;
                        continue;
                    } else {
                        throw newDatatypeException(i
                                "Expected a double quote or a token character but saw ",
                                        c" instead.");
                    }
                case :
                    if (c == '\\') {
                        state = .;
                        continue;
                    } else if (c == '\"') {
                        state = .;
                        continue;
                    } else if (isQDTextChar(c)) {
                        continue;
                    } else {
                        throw newDatatypeException(i
                                "Expected a non-control ASCII character but saw ",
                                        c" instead.");
                    }
                case :
                    if (c <= 127) {
                        state = .;
                        continue;
                    } else {
                        throw newDatatypeException(i
                                "Expected an ASCII character but saw ",
                                        c" instead.");
                    }
                case :
                    if (c == ';') {
                        state = .;
                        continue;
                    } else if (isWhitespace(c)) {
                        state = .;
                        continue;
                    } else if (c == ',') {
                         = sb.substring(0, sb.length() - 1);
                         = is;
                        return;
                    } else {
                        throw newDatatypeException(i
                                "Expected an ASCII character but saw ",
                                        c" instead.");
                    }
                case :
                    if (isTokenChar(c)) {
                        continue;
                    } else if (c == ';') {
                        state = .;
                        continue;
                    } else if (isWhitespace(c)) {
                        state = .;
                        continue;
                    } else if (c == ',') {
                         = sb.substring(0, sb.length() - 1);
                         = is;
                        return;
                    } else {
                        throw newDatatypeException(i
                                "Expected a token character, whitespace, a semicolon, or a comma but saw ",
                                        c" instead.");
                    }
            }
        }
    }

    

Throws:
java.io.IOException, MalformedURLException
    public DataUri(String urithrows IOExceptionMalformedURLException {
        IRIFactory fac = new IRIFactory();
        fac.shouldViolation(truefalse);
        fac.securityViolation(truefalse);
        fac.dnsViolation(truefalse);
        fac.mintingViolation(falsefalse);
        fac.useSpecificationIRI(true);
        init(fac.construct(uri));
    }

    

Throws:
java.io.IOException, MalformedURLException
    public DataUri(IRI urithrows IOExceptionMalformedURLException {
        init(uri);
    }
    private IOException newDatatypeException(int iString headchar cString tail) {
        return new DataUriException(iheadctail);
    }
    private boolean isQDTextChar(char c) {
        return (c >= ' ' && c <= 126) || (c == '\n') || (c == '\r')
                || (c == '\t');
    }
    private boolean isTokenChar(char c) {
        return (c >= 33 && c <= 126)
                && !(c == '(' || c == ')' || c == '<' || c == '>' || c == '@'
                        || c == ',' || c == ';' || c == ':' || c == '\\'
                        || c == '\"' || c == '/' || c == '[' || c == ']'
                        || c == '?' || c == '=' || c == '{' || c == '}');
    }

    
Checks if a UTF-16 code unit represents a whitespace character (U+0020, U+0009, U+000D or U+000A).

Parameters:
c the code unit
Returns:
true if whitespace, false otherwise
    private boolean isWhitespace(char c) {
        return c == ' ' || c == '\t' || c == '\n' || c == '\r';
    }

    
Returns the contentType.

Returns:
the contentType
    public String getContentType() {
        return ;
    }

    
Returns the inputStream.

Returns:
the inputStream
    public InputStream getInputStream() {
        return ;
    }
New to GrepCode? Check out our FAQ X