blob: 36867949effff43c4cfd12085fcb9e96b5142004 [file] [log] [blame]
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001/* Jackson JSON-processor.
2 *
3 * Copyright (c) 2007- Tatu Saloranta, tatu.saloranta@iki.fi
Tatu Salorantaf15531c2011-12-22 23:00:40 -08004 */
5package com.fasterxml.jackson.core;
6
7/**
Tatu Saloranta41bd0eb2012-10-05 15:01:05 -07008 * Container for commonly used Base64 variants:
9 *<ul>
10 * <li> {@link #MIME}
11 * <li> {@link #MIME_NO_LINEFEEDS}
12 * <li> {@link #PEM}
13 * <li> {@link #MODIFIED_FOR_URL}
14 * </ul>
Tatu Salorantaf15531c2011-12-22 23:00:40 -080015 *
16 * @author Tatu Saloranta
17 */
18public final class Base64Variants
19{
20 final static String STD_BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
21
22 /**
23 * This variant is what most people would think of "the standard"
24 * Base64 encoding.
25 *<p>
Tatu Saloranta3dcedd22015-01-10 20:15:06 -080026 * See <a href="http://en.wikipedia.org/wiki/Base64">wikipedia Base64 entry</a> for details.
Tatu Salorantaf15531c2011-12-22 23:00:40 -080027 *<p>
28 * Note that although this can be thought of as the standard variant,
29 * it is <b>not</b> the default for Jackson: no-linefeeds alternative
30 * is because of JSON requirement of escaping all linefeeds.
31 */
32 public final static Base64Variant MIME;
33 static {
34 MIME = new Base64Variant("MIME", STD_BASE64_ALPHABET, true, '=', 76);
35 }
36
37 /**
38 * Slightly non-standard modification of {@link #MIME} which does not
39 * use linefeeds (max line length set to infinite). Useful when linefeeds
40 * wouldn't work well (possibly in attributes), or for minor space savings
41 * (save 1 linefeed per 76 data chars, ie. ~1.4% savings).
42 */
43 public final static Base64Variant MIME_NO_LINEFEEDS;
44 static {
45 MIME_NO_LINEFEEDS = new Base64Variant(MIME, "MIME-NO-LINEFEEDS", Integer.MAX_VALUE);
46 }
47
48 /**
49 * This variant is the one that predates {@link #MIME}: it is otherwise
50 * identical, except that it mandates shorter line length.
51 */
52 public final static Base64Variant PEM = new Base64Variant(MIME, "PEM", true, '=', 64);
53
54 /**
55 * This non-standard variant is usually used when encoded data needs to be
56 * passed via URLs (such as part of GET request). It differs from the
57 * base {@link #MIME} variant in multiple ways.
58 * First, no padding is used: this also means that it generally can not
59 * be written in multiple separate but adjacent chunks (which would not
60 * be the usual use case in any case). Also, no linefeeds are used (max
61 * line length set to infinite). And finally, two characters (plus and
62 * slash) that would need quoting in URLs are replaced with more
63 * optimal alternatives (hyphen and underscore, respectively).
64 */
65 public final static Base64Variant MODIFIED_FOR_URL;
66 static {
Gonçalo Silva3c4f7872014-01-25 17:04:52 +000067 StringBuilder sb = new StringBuilder(STD_BASE64_ALPHABET);
Tatu Salorantaf15531c2011-12-22 23:00:40 -080068 // Replace plus with hyphen, slash with underscore (and no padding)
69 sb.setCharAt(sb.indexOf("+"), '-');
70 sb.setCharAt(sb.indexOf("/"), '_');
71 /* And finally, let's not split lines either, wouldn't work too
72 * well with URLs
73 */
74 MODIFIED_FOR_URL = new Base64Variant("MODIFIED-FOR-URL", sb.toString(), false, Base64Variant.PADDING_CHAR_NONE, Integer.MAX_VALUE);
75 }
76
77 /**
78 * Method used to get the default variant ("MIME_NO_LINEFEEDS") for cases
79 * where caller does not explicitly specify the variant.
80 * We will prefer no-linefeed version because linefeeds in JSON values
81 * must be escaped, making linefeed-containing variants sub-optimal.
82 */
83 public static Base64Variant getDefaultVariant() {
84 return MIME_NO_LINEFEEDS;
85 }
Tatu Saloranta41bd0eb2012-10-05 15:01:05 -070086
87 /**
88 * @since 2.1
89 */
90 public static Base64Variant valueOf(String name) throws IllegalArgumentException
91 {
92 if (MIME._name.equals(name)) {
93 return MIME;
94 }
95 if (MIME_NO_LINEFEEDS._name.equals(name)) {
96 return MIME_NO_LINEFEEDS;
97 }
98 if (PEM._name.equals(name)) {
99 return PEM;
100 }
101 if (MODIFIED_FOR_URL._name.equals(name)) {
102 return MODIFIED_FOR_URL;
103 }
104 if (name == null) {
105 name = "<null>";
106 } else {
107 name = "'"+name+"'";
108 }
109 throw new IllegalArgumentException("No Base64Variant with name "+name);
110 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800111}