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 org.apache.commons.codec.binary.Base64; 008 009 /* 010 Copyright (c) 2012, 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 data URI. 041 * </p> 042 * <p> 043 * Example: <code>data:image/jpeg;base64,<base64-encoded text></code> 044 * </p> 045 * @author Michael Angstadt 046 */ 047 public class DataUri { 048 protected static final Pattern regex = Pattern.compile("^data:(.*?);base64,(.*)", Pattern.CASE_INSENSITIVE); 049 protected byte[] data; 050 protected String contentType; 051 052 /** 053 * @param data the binary data 054 * @param contentType the content type (e.g. "image/jpeg") 055 */ 056 public DataUri(String contentType, byte[] data) { 057 this.contentType = contentType; 058 this.data = data; 059 } 060 061 /** 062 * @param uri the data URI to parse 063 * @throws IllegalArgumentException if the given URI is not a valid data URI 064 */ 065 public DataUri(String uri) { 066 Matcher m = regex.matcher(uri); 067 if (m.find()) { 068 contentType = m.group(1); 069 data = Base64.decodeBase64(m.group(2)); 070 } else { 071 throw new IllegalArgumentException("Invalid data URI: " + uri); 072 } 073 } 074 075 /** 076 * Gets the binary data. 077 * @return the binary data or null if not set 078 */ 079 public byte[] getData() { 080 return data; 081 } 082 083 /** 084 * Sets the binary data. 085 * @param data the binary data 086 */ 087 public void setData(byte[] data) { 088 this.data = data; 089 } 090 091 /** 092 * Sets the content type. 093 * @return the content type (e.g. "image/jpeg") 094 */ 095 public String getContentType() { 096 return contentType; 097 } 098 099 /** 100 * Sets the content type. 101 * @param contentType the content type (e.g. "image/jpeg") 102 */ 103 public void setContentType(String contentType) { 104 this.contentType = contentType; 105 } 106 107 /** 108 * Creates a {@link URI} object from this data URI. 109 * @return the {@link URI} object 110 */ 111 public URI toUri() { 112 return URI.create(toString()); 113 } 114 115 @Override 116 public String toString() { 117 return "data:" + contentType + ";base64," + Base64.encodeBase64String(data); 118 } 119 }