Skip to content
This repository has been archived by the owner on Nov 16, 2018. It is now read-only.

Commit

Permalink
Removed confusing charset option
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas C. Zakas committed Oct 30, 2009
1 parent 12b2506 commit dc1ea63
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 132 deletions.
8 changes: 4 additions & 4 deletions src/net/nczonline/web/datauri/DataURI.java
Expand Up @@ -100,17 +100,17 @@ public static void main(String[] args) {
if (verbose){
System.err.println("[INFO] Output file is '" + (new File(outputFilename)).getAbsolutePath() + "'");
}
out = new OutputStreamWriter(new FileOutputStream(outputFilename), charset);
out = new OutputStreamWriter(new FileOutputStream(outputFilename), "UTF-8");
}

//set verbose option
DataURIGenerator.setVerbose(verbose);

//determine if the filename is a local file or a URL
if (inputFilename.startsWith("http://")){
DataURIGenerator.generate(new URL(inputFilename), out, mimeType, charset);
DataURIGenerator.generate(new URL(inputFilename), out, mimeType);
} else {
DataURIGenerator.generate(new File(inputFilename), out, mimeType, charset);
DataURIGenerator.generate(new File(inputFilename), out, mimeType);
}

} catch (CmdLineParser.OptionException e) {
Expand Down Expand Up @@ -140,7 +140,7 @@ private static void usage() {

+ "Global Options\n"
+ " -h, --help Displays this information.\n"
+ " --charset <charset> Character set to encode into the data URI.\n"
+ " --charset <charset> Character set of the input file.\n"
+ " -v, --verbose Display informational messages and warnings.\n"
+ " -m, --mime <type> Mime type to encode into the data URI.\n"
+ " -o <file> Place the output into <file>. Defaults to stdout.");
Expand Down
152 changes: 24 additions & 128 deletions src/net/nczonline/web/datauri/DataURIGenerator.java
Expand Up @@ -32,7 +32,6 @@
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;

/**
* Generator for Data URIs.
Expand Down Expand Up @@ -96,20 +95,8 @@ public static void generate(File file, Writer out) throws IOException {
* @throws java.io.IOException
*/
public static void generate(File file, Writer out, String mimeType) throws IOException {
generate(file, out, null, null);
}

/**
* Generates a data URI from a file, outputting it to the given writer.
* @param file The file from which to generate the data URI.
* @param out Where to output the data URI.
* @param mimeType The MIME type to use for the data URI.
* @param charset The charset to use for the data URI.
* @throws java.io.IOException
*/
public static void generate(File file, Writer out, String mimeType, String charset) throws IOException {
generateDataURI(file, out, mimeType, charset);
}
generateDataURI(file, out, mimeType);
}

//--------------------------------------------------------------------------
// Generate data URIs from a URL
Expand All @@ -134,21 +121,9 @@ public static void generate(URL url, Writer out) throws IOException {
* @throws java.io.IOException
*/
public static void generate(URL url, Writer out, String mimeType) throws IOException {
generate(url, out, null, null);
generateDataURI(url, out, mimeType);
}

/**
* Generates a data URI from a URL, outputting it to the given writer.
* @param url The URL form which to generate the data URI.
* @param out Where to output the data URI.
* @param mimeType The MIME type to use for the data URI.
* @param charset The charset to use for the data URI.
* @throws java.io.IOException
*/
public static void generate(URL url, Writer out, String mimeType, String charset) throws IOException {
generateDataURI(url, out, mimeType, charset);
}

//--------------------------------------------------------------------------
// Helper methods
//--------------------------------------------------------------------------
Expand All @@ -161,7 +136,7 @@ public static void generate(URL url, Writer out, String mimeType, String charset
* @param charset The character set to specify in the data URI.
* @throws java.io.IOException
*/
private static void generateDataURI(File file, Writer out, String mimeType, String charset) throws IOException{
private static void generateDataURI(File file, Writer out, String mimeType) throws IOException{

//read the bytes from the file
InputStream in = new FileInputStream(file);
Expand All @@ -170,22 +145,20 @@ private static void generateDataURI(File file, Writer out, String mimeType, Stri
in.close();

//verify MIME type and charset
mimeType = getMimeType(file.getName(), mimeType);
charset = getCharsetForFilename(file.getName(), charset);
mimeType = getMimeType(file.getName(), mimeType);

//actually write
generateDataURI(bytes, out, mimeType, charset);
generateDataURI(bytes, out, mimeType);
}

/**
* Generates a data URI from the specified URL and outputs to the given writer.
* @param url The URL to from which to create a data URI.
* @param out Where to output the data URI.
* @param mimeType The MIME type to specify in the data URI.
* @param charset The character set to specify in the data URI.
* @throws java.io.IOException
*/
private static void generateDataURI(URL url, Writer out, String mimeType, String charset) throws IOException{
private static void generateDataURI(URL url, Writer out, String mimeType) throws IOException{

//get information about the URL
URLConnection conn = url.openConnection();
Expand All @@ -194,31 +167,17 @@ private static void generateDataURI(URL url, Writer out, String mimeType, String
if (mimeType == null){
mimeType = getMimeType(url.getFile(), conn.getContentType());
if (verbose){
System.err.println("[INFO] No MIME type provided, using '" + mimeType + "'.");
System.err.println("[INFO] No MIME type provided, using detected type of '" + mimeType + "'.");
}
}

//sometimes charset is in the MIME type
if (mimeType.indexOf("; charset=") > -1){

//assign charset
if (charset == null){
charset = mimeType.substring(mimeType.indexOf("=") + 1);
if (verbose){
System.err.println("[INFO] No charset provided, using '" + charset + "' (from MIME type).");
}
}

//remove from MIME type
mimeType = mimeType.substring(0, mimeType.indexOf(";"));
if (verbose){
System.err.println("[INFO] Removed charset from MIME type, MIME type is now '" + mimeType + "'.");
}
if (mimeType.indexOf("; charset=") > -1){
mimeType = mimeType.replace(" ", ""); //remove the space
} else {
mimeType = getMimeTypeWithCharset(mimeType);
}

//verify charset
charset = getCharsetForMIMEType(mimeType, charset);

//read the bytes from the URL
InputStream in = conn.getInputStream();
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
Expand All @@ -232,7 +191,7 @@ private static void generateDataURI(URL url, Writer out, String mimeType, String
in.close();

//actually write
generateDataURI(byteStream.toByteArray(), out, mimeType, charset);
generateDataURI(byteStream.toByteArray(), out, mimeType);
}

/**
Expand All @@ -243,7 +202,7 @@ private static void generateDataURI(URL url, Writer out, String mimeType, String
* @param charset The character set to specify in the data URI.
* @throws java.io.IOException
*/
private static void generateDataURI(byte[] bytes, Writer out, String mimeType, String charset) throws IOException {
private static void generateDataURI(byte[] bytes, Writer out, String mimeType) throws IOException {

//create the output
StringBuffer buffer = new StringBuffer();
Expand All @@ -252,11 +211,6 @@ private static void generateDataURI(byte[] bytes, Writer out, String mimeType, S
//add MIME type
buffer.append(mimeType);

//add charset if necessary
if (charset != null){
buffer.append(";charset=" + charset);
}

//output base64-encoding
buffer.append(";base64,");
buffer.append(new String(Base64.encodeBytes(bytes)));
Expand Down Expand Up @@ -308,7 +262,7 @@ private static String getMimeType(String filename, String mimeType) throws IOExc
if (imageTypes.containsKey(type)){
mimeType = (String) imageTypes.get(type);
} else if (textTypes.containsKey(type)){
mimeType = (String) textTypes.get(type);
mimeType = (String) textTypes.get(type) + ";charset=UTF-8";
} else {
throw new IOException("No MIME type provided and MIME type couldn't be automatically determined.");
}
Expand All @@ -321,78 +275,20 @@ private static String getMimeType(String filename, String mimeType) throws IOExc
return mimeType;
}

/**
* Determines the charset to use for the given filename. If a charset is
* passed in, then that is used by default. If the filename represents an
* image, this method always returns null because no charset should be used.
* @param filename The filename to check.
* @param charset The provided charset.
* @return The charset string or null if no charset should be used.
*/
private static String getCharsetForFilename(String filename, String charset) {
if (charset != null){

if (Charset.isSupported(charset)){

if (isImageFile(filename)){
if (verbose){
System.err.println("[INFO] Image file detected, skipping charset '" + charset + "'.");
}
charset = null;
} else {
if (verbose){
System.err.println("[INFO] Using charset '" + charset + "'.");
}
}
private static String getMimeTypeWithCharset(String mimeType){


} else {
charset = null;
if (verbose){
System.err.println("[INFO] Charset '" + charset + "' is invalid, skipping.");
}
}

} else {
if (imageTypes.containsValue(mimeType)){
if (verbose){
System.err.println("[INFO] Charset not specified, skipping.");
}
}

return charset;
}

private static String getCharsetForMIMEType(String mimeType, String charset){
if (charset != null){

if (Charset.isSupported(charset)){

if (imageTypes.containsValue(mimeType)){
if (verbose){
System.err.println("[INFO] Image file detected, skipping charset '" + charset + "'.");
}
charset = null;
} else {
if (verbose){
System.err.println("[INFO] Using charset '" + charset + "'.");
}
}


} else {
charset = null;
if (verbose){
System.err.println("[INFO] Charset '" + charset + "' is invalid, skipping.");
}
}

System.err.println("[INFO] Image file detected, skipping charset.");
}
return mimeType;
} else {
if (verbose){
System.err.println("[INFO] Charset not specified, skipping.");
}
System.err.println("[INFO] Using charset 'UTF-8'.");
}
return mimeType + ";charset=UTF-8";
}

return charset;

}

}

0 comments on commit dc1ea63

Please sign in to comment.