Debian Codegraph @

src/com/drew/metadata/iptc/Iso2022Converter.java

raw symbols SCIP
1 /*
2 * Copyright 2002-2019 Drew Noakes and contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * More information about this project is available at:
17 *
18 * https://drewnoakes.com/code/exif/
19 * https://github.com/drewnoakes/metadata-extractor
20 */
21 package com.drew.metadata.iptc;
22
23 import com.drew.lang.annotations.NotNull;
24 import com.drew.lang.annotations.Nullable;
25
26 import java.nio.ByteBuffer;
27 import java.nio.charset.CharacterCodingException;
28 import java.nio.charset.Charset;
29 import java.nio.charset.CharsetDecoder;
30
31 public final class Iso2022Converter
32 {
33 private static final String ISO_8859_1 = "ISO-8859-1";
34 private static final String UTF_8 = "UTF-8";
35
36 private static final byte LATIN_CAPITAL_A = 0x41;
37 private static final int DOT = 0xe280a2;
38 private static final byte LATIN_CAPITAL_G = 0x47;
39 private static final byte PERCENT_SIGN = 0x25;
40 private static final byte MINUS_SIGN = 0x2D;
41 private static final byte DOT_SIGN = 0x2E;
42 private static final byte ESC = 0x1B;
43
44 /**
45 * Converts the given ISO2022 char set to a Java charset name.
46 * A reference of valid charsets can be found here: http://nozer0.github.io/en/technology/system/character-encoding/#ISO/IEC%202022
47 *
48 * @param bytes string data encoded using ISO2022
49 * @return the Java charset name as a string, or <code>null</code> if the conversion was not possible
50 */
51 @Nullable
52 public static String convertISO2022CharsetToJavaCharset(@NotNull final byte[] bytes)
53 {
54 if (bytes.length > 2 && bytes[0] == ESC && bytes[1] == PERCENT_SIGN && bytes[2] == LATIN_CAPITAL_G)
55 return UTF_8;
56
57 if (bytes.length > 2 && bytes[0] == ESC && bytes[1] == DOT_SIGN && bytes[2] == LATIN_CAPITAL_A)
58 return ISO_8859_1;
59
60 if (bytes.length > 3 && bytes[0] == ESC && (bytes[3] & 0xFF | ((bytes[2] & 0xFF) << 8) | ((bytes[1] & 0xFF) << 16)) == DOT && bytes[4] == LATIN_CAPITAL_A)
61 return ISO_8859_1;
62
63 if (bytes.length > 2 && bytes[0] == ESC && bytes[1] == MINUS_SIGN && bytes[2] == LATIN_CAPITAL_A)
64 return ISO_8859_1;
65
66 return null;
67 }
68
69 /**
70 * Attempts to guess the {@link Charset} of a string provided as a byte array.
71 * <p>
72 * Charsets trialled are, in order:
73 * <ul>
74 * <li>UTF-8</li>
75 * <li><code>System.getProperty("file.encoding")</code></li>
76 * <li>ISO-8859-1</li>
77 * </ul>
78 * <p>
79 * Its only purpose is to guess the Charset if and only if IPTC tag coded character set is not set. If the
80 * encoding is not UTF-8, the tag should be set. Otherwise it is bad practice. This method tries to
81 * workaround this issue since some metadata manipulating tools do not prevent such bad practice.
82 * <p>
83 * About the reliability of this method: The check if some bytes are UTF-8 or not has a very high reliability.
84 * The two other checks are less reliable.
85 *
86 * @param bytes some text as bytes
87 * @return the name of the encoding or null if none could be guessed
88 */
89 @Nullable
90 static Charset guessCharSet(@NotNull final byte[] bytes)
91 {
92 String[] encodings = { UTF_8, System.getProperty("file.encoding"), ISO_8859_1 };
93
94 for (String encoding : encodings)
95 {
96 Charset charset = Charset.forName(encoding);
97 CharsetDecoder cs = charset.newDecoder();
98
99 try {
100 cs.decode(ByteBuffer.wrap(bytes));
101 return charset;
102 } catch (CharacterCodingException e) {
103 // fall through...
104 }
105 }
106
107 // No encodings succeeded. Return null.
108 return null;
109 }
110
111 private Iso2022Converter()
112 {}
113 }