Crossfire JXClient, Trunk  R20561
Codec.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.util;
23 
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 import org.jetbrains.annotations.NotNull;
27 
34 @SuppressWarnings("HtmlTagCanBeJavadocTag")
35 public class Codec {
36 
41  @NotNull
42  private static final Pattern[] PATTERNS_ENCODE = {
43  Pattern.compile("\\\\"),
44  Pattern.compile("\r"),
45  Pattern.compile("\n"),
46  };
47 
51  @NotNull
52  private static final String[] REPLACEMENTS_ENCODE = {
53  Matcher.quoteReplacement("\\\\"),
54  Matcher.quoteReplacement("\\r"),
55  Matcher.quoteReplacement("\\n"),
56  };
57 
62  @NotNull
63  private static final Pattern[] PATTERNS_DECODE = {
64  Pattern.compile("\\\\n"),
65  Pattern.compile("\\\\r"),
66  Pattern.compile("\\\\\\\\"),
67  };
68 
72  @NotNull
73  private static final String[] REPLACEMENTS_DECODE = {
74  Matcher.quoteReplacement("\n"),
75  Matcher.quoteReplacement("\r"),
76  Matcher.quoteReplacement("\\"),
77  };
78 
82  private Codec() {
83  }
84 
91  @NotNull
92  public static String encode(@NotNull final String str) {
93  assert PATTERNS_ENCODE.length == REPLACEMENTS_ENCODE.length;
94  String tmp = str;
95  for (int i = 0; i < PATTERNS_ENCODE.length; i++) {
96  tmp = PATTERNS_ENCODE[i].matcher(tmp).replaceAll(REPLACEMENTS_ENCODE[i]);
97  }
98  return tmp;
99  }
100 
107  @NotNull
108  public static String decode(@NotNull final String str) {
109  assert PATTERNS_DECODE.length == REPLACEMENTS_DECODE.length;
110  String tmp = str;
111  for (int i = 0; i < PATTERNS_DECODE.length; i++) {
112  tmp = PATTERNS_DECODE[i].matcher(tmp).replaceAll(REPLACEMENTS_DECODE[i]);
113  }
114  return tmp;
115  }
116 
117 }
static String encode(@NotNull final String str)
Encodes a string to make it fit into one line.
Definition: Codec.java:92
Codec()
Private constructor to prevent instantiation.
Definition: Codec.java:82
static String decode(@NotNull final String str)
Decodes a string which was encoded by encode(String).
Definition: Codec.java:108
Utility class to encode arbitrary Strings to fit in a single text line.
Definition: Codec.java:35