001    package ezvcard.util;
002    
003    import java.net.URI;
004    import java.util.regex.Matcher;
005    import java.util.regex.Pattern;
006    
007    import ezvcard.util.org.apache.commons.codec.binary.Base64;
008    
009    /*
010     Copyright (c) 2013, Michael Angstadt
011     All rights reserved.
012    
013     Redistribution and use in source and binary forms, with or without
014     modification, are permitted provided that the following conditions are met: 
015    
016     1. Redistributions of source code must retain the above copyright notice, this
017     list of conditions and the following disclaimer. 
018     2. Redistributions in binary form must reproduce the above copyright notice,
019     this list of conditions and the following disclaimer in the documentation
020     and/or other materials provided with the distribution. 
021    
022     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
023     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
024     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
025     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
026     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
027     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
029     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
030     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
031     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032    
033     The views and conclusions contained in the software and documentation are those
034     of the authors and should not be interpreted as representing official policies, 
035     either expressed or implied, of the FreeBSD Project.
036     */
037    
038    /**
039     * <p>
040     * Represents a URI for encoding binary data.
041     * </p>
042     * <p>
043     * Example: {@code data:image/jpeg;base64,[base64 string]}
044     * </p>
045     * @author Michael Angstadt
046     */
047    public final class DataUri {
048            private static final Pattern regex = Pattern.compile("^data:(.*?);base64,(.*)", Pattern.CASE_INSENSITIVE);
049            private final byte[] data;
050            private final String contentType;
051    
052            /**
053             * Creates a data URI.
054             * @param contentType the content type (e.g. "image/jpeg")
055             * @param data the binary data
056             */
057            public DataUri(String contentType, byte[] data) {
058                    this.contentType = contentType;
059                    this.data = data;
060            }
061    
062            /**
063             * Parses a data URI string.
064             * @param uri the data URI to parse (e.g.
065             * "data:image/jpeg;base64,[base64 string]")
066             * @throws IllegalArgumentException if the given URI is not a valid data URI
067             */
068            public DataUri(String uri) {
069                    Matcher m = regex.matcher(uri);
070                    if (!m.find()) {
071                            throw new IllegalArgumentException("Invalid data URI: " + uri);
072                    }
073    
074                    contentType = m.group(1);
075                    data = Base64.decodeBase64(m.group(2));
076            }
077    
078            /**
079             * Gets the binary data.
080             * @return the binary data
081             */
082            public byte[] getData() {
083                    return data;
084            }
085    
086            /**
087             * Sets the content type.
088             * @return the content type (e.g. "image/jpeg")
089             */
090            public String getContentType() {
091                    return contentType;
092            }
093    
094            /**
095             * Creates a {@link URI} object from this data URI.
096             * @return the {@link URI} object
097             */
098            public URI toUri() {
099                    return URI.create(toString());
100            }
101    
102            @Override
103            public String toString() {
104                    return "data:" + contentType + ";base64," + Base64.encodeBase64String(data);
105            }
106    }