From: Roland Haeder Date: Mon, 27 Jul 2015 13:33:58 +0000 (+0200) Subject: Special characters cannot be securely written with direct writeBytes() and readLine... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=6a19807c780214298437fa62514ed708008b12f1;p=addressbook-swing.git Special characters cannot be securely written with direct writeBytes() and readLine() method, but BASE64 works flawless ... Signed-off-by:Roland Häder --- 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 b09fe8f..49b3128 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); }