Crossfire JXClient, Trunk
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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.util;
24 
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 import org.jetbrains.annotations.NotNull;
28 
35 @SuppressWarnings("HtmlTagCanBeJavadocTag")
36 public class Codec {
37 
42  @NotNull
43  private static final Pattern @NotNull [] PATTERNS_ENCODE = { //
44  Pattern.compile("\\\\"), //
45  Pattern.compile("\r"), //
46  Pattern.compile("\n"), //
47  };
48 
52  @NotNull
53  private static final String @NotNull [] REPLACEMENTS_ENCODE = { //
54  Matcher.quoteReplacement("\\\\"), //
55  Matcher.quoteReplacement("\\r"), //
56  Matcher.quoteReplacement("\\n"), //
57  };
58 
63  @NotNull
64  private static final Pattern @NotNull [] PATTERNS_DECODE = { //
65  Pattern.compile("\\\\n"), //
66  Pattern.compile("\\\\r"), //
67  Pattern.compile("\\\\\\\\"), //
68  };
69 
73  @NotNull
74  private static final String @NotNull [] REPLACEMENTS_DECODE = { //
75  Matcher.quoteReplacement("\n"), //
76  Matcher.quoteReplacement("\r"), //
77  Matcher.quoteReplacement("\\"), //
78  };
79 
83  private Codec() {
84  }
85 
92  @NotNull
93  public static String encode(@NotNull final String str) {
94  assert PATTERNS_ENCODE.length == REPLACEMENTS_ENCODE.length;
95  String tmp = str;
96  for (int i = 0; i < PATTERNS_ENCODE.length; i++) {
97  tmp = PATTERNS_ENCODE[i].matcher(tmp).replaceAll(REPLACEMENTS_ENCODE[i]);
98  }
99  return tmp;
100  }
101 
108  @NotNull
109  public static String decode(@NotNull final String str) {
110  assert PATTERNS_DECODE.length == REPLACEMENTS_DECODE.length;
111  String tmp = str;
112  for (int i = 0; i < PATTERNS_DECODE.length; i++) {
113  tmp = PATTERNS_DECODE[i].matcher(tmp).replaceAll(REPLACEMENTS_DECODE[i]);
114  }
115  return tmp;
116  }
117 
118 }
com.realtime.crossfire.jxclient.util.Codec.Codec
Codec()
Definition: Codec.java:83
com.realtime.crossfire.jxclient.util.Codec.encode
static String encode(@NotNull final String str)
Definition: Codec.java:93
com.realtime.crossfire.jxclient.util.Codec
Definition: Codec.java:36
com.realtime.crossfire.jxclient.util.Codec.decode
static String decode(@NotNull final String str)
Definition: Codec.java:109