|
15 | 15 | import javafx.scene.layout.VBox;
|
16 | 16 | import javafx.stage.Stage;
|
17 | 17 |
|
18 |
| -import java.io.IOException; |
19 |
| -import java.io.PrintWriter; |
| 18 | +import java.io.*; |
20 | 19 | import java.nio.charset.StandardCharsets;
|
21 | 20 | import java.nio.file.Paths;
|
22 | 21 | import java.util.ArrayList;
|
@@ -97,54 +96,59 @@ public void start(Stage primaryStage) throws Exception {
|
97 | 96 | private void saveBytesToFile(String text) throws IOException {
|
98 | 97 | int counter = 0;
|
99 | 98 | ArrayList<String> splitBytes = new ArrayList<>();
|
100 |
| - String temp = ""; |
| 99 | + StringBuilder strByte = new StringBuilder(); |
101 | 100 | for (char ch : text.toCharArray()) {
|
102 | 101 | if (counter < 8) {
|
103 |
| - temp += ch; |
| 102 | + strByte.append(ch); |
104 | 103 | counter++;
|
105 | 104 | } else {
|
106 |
| - splitBytes.add(temp); |
| 105 | + splitBytes.add(strByte.toString()); |
107 | 106 | counter = 0;
|
108 |
| - temp = ""; |
| 107 | + strByte = new StringBuilder(); |
109 | 108 | }
|
110 | 109 | }
|
111 |
| -PrintWriter printWriter = new PrintWriter(filePath, StandardCharsets.UTF_8.name()); |
| 110 | + FileOutputStream fOs = new FileOutputStream(filePath); |
| 111 | + StringBuilder stringBuilder = new StringBuilder(); |
| 112 | + OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fOs); |
112 | 113 | char ch = 0;
|
113 | 114 | for (String bits : splitBytes) {
|
114 |
| - ch = getCharFromByte(bits); |
115 |
| - printWriter.write(ch); |
| 115 | + ch = (char) getCharFromByte(bits); |
| 116 | + stringBuilder.append(ch); |
116 | 117 | }
|
117 |
| - printWriter.flush(); |
118 |
| - printWriter.close(); |
| 118 | + |
| 119 | + outputStreamWriter.write(stringBuilder.toString()); |
| 120 | + outputStreamWriter.close(); |
119 | 121 |
|
120 | 122 | }
|
121 | 123 |
|
122 | 124 | private String readBytesFromFile() throws IOException {
|
123 | 125 | StringBuilder result = new StringBuilder();
|
124 |
| - Scanner in = new Scanner(Paths.get(filePath), StandardCharsets.UTF_8.name()); |
125 |
| - while (in.hasNextLine()) { |
126 |
| - String line = in.nextLine(); |
127 |
| - for (char ch : line.toCharArray()) { |
128 |
| - result.append(getBits(ch)); |
129 |
| - } |
| 126 | + FileInputStream fIs = new FileInputStream(filePath); |
| 127 | + InputStreamReader iSr = new InputStreamReader(fIs); |
| 128 | + Reader in = new BufferedReader(iSr); |
| 129 | + int ch; |
| 130 | + while ((ch = in.read()) != -1) { |
| 131 | + String valueAsByte = getBits(ch); |
| 132 | + result.append(valueAsByte); |
130 | 133 | }
|
131 | 134 | in.close();
|
132 | 135 |
|
133 | 136 | return result.toString();
|
| 137 | + |
134 | 138 | }
|
135 | 139 |
|
136 | 140 | public static String getBits(int value) {
|
137 |
| - String bits = ""; |
138 |
| - for (int i = 0; i < 8; i++) { |
139 |
| - bits += (value & 1); |
140 |
| - value >>= 1; |
| 141 | + StringBuilder bits = new StringBuilder(); |
| 142 | + long i; |
| 143 | + for (i = 128; i > 0; i /= 2) { |
| 144 | + bits.append((value & i) != 0 ? "1" : "0"); |
141 | 145 | }
|
142 |
| - return bits; |
| 146 | + return bits.toString(); |
143 | 147 | }
|
144 | 148 |
|
145 |
| - public static char getCharFromByte(String value) { |
| 149 | + public static int getCharFromByte(String value) { |
146 | 150 | int numVal = Integer.parseInt(value, 2);
|
147 |
| - return (char) numVal; |
| 151 | + return numVal; |
148 | 152 | }
|
149 | 153 |
|
150 | 154 |
|
|
0 commit comments