From 6a19807c780214298437fa62514ed708008b12f1 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Mon, 27 Jul 2015 15:33:58 +0200 Subject: [PATCH] =?utf8?q?Special=20characters=20cannot=20be=20securely=20?= =?utf8?q?written=20with=20direct=20writeBytes()=20and=20readLine()=20meth?= =?utf8?q?od,=20but=20BASE64=20works=20flawless=20...=20Signed-off-by:Rola?= =?utf8?q?nd=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../backend/csv/CsvDatabaseBackend.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java index b09fe8fe..49b31285 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.RandomAccessFile; import java.text.MessageFormat; import java.util.ArrayList; +import java.util.Base64; import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; @@ -59,7 +60,7 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBacken this.setTableName(tableName); // Construct file name - String fileName = String.format("data/table_%s.csv", tableName); + String fileName = String.format("data/table_%s.b64", tableName); // Debug message this.getLogger().debug(MessageFormat.format("Trying to open file {0} ...", fileName)); @@ -171,8 +172,11 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBacken // Debug message this.getLogger().debug(MessageFormat.format("str({0})={1}", str.length(), str)); + // Encode line in BASE-64 + byte[] encoded = Base64.getEncoder().encode(str.trim().getBytes()); + // The string is now a valid CSV string - this.getStorageFile().writeBytes(str); + this.getStorageFile().write(encoded); } /** @@ -298,13 +302,19 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBacken // Get next token String token = tokenizer.nextToken(); + // If char " is at pos 2 (0,1,2), then cut it of there + if ((token.charAt(0) != '"') && (token.charAt(2) == '"')) { + // UTF-8 writer characters found + token = token.substring(2); + } + // Debug message this.getLogger().debug(MessageFormat.format("token={0}", token)); // Verify token, it must have double-quotes on each side if ((!token.startsWith("\"")) || (!token.endsWith("\""))) { // Something bad was read - throw new BadTokenException(MessageFormat.format("Token {0} has not double-quotes on both ends.", token)); + throw new BadTokenException(MessageFormat.format("Token {0} at position {1} has not double-quotes on both ends.", token, count)); } // All fine, so remove it @@ -516,7 +526,13 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBacken String input = null; try { - input = this.getStorageFile().readLine(); + String base64 = this.getStorageFile().readLine(); + + // Decode BASE-64 + byte[] decoded = Base64.getDecoder().decode(base64); + + // Convert to string + input = new String(decoded); } catch (final IOException ex) { this.getLogger().catching(ex); } -- 2.39.5