1
0
forked from kalmar/DALGLI0

charset update

This commit is contained in:
Hubert Tylkowski 2018-06-20 11:45:59 +02:00
parent 744f9793c5
commit 841b8b4590
9 changed files with 473 additions and 45 deletions

View File

@ -0,0 +1 @@
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>

12
Zadanie-03/.idea/misc.xml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="ProjectKey">
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Zadanie-03.iml" filepath="$PROJECT_DIR$/Zadanie-03.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,3 @@
<template>
<input-field default="com.company">IJ_BASE_PACKAGE</input-field>
</template>

View File

@ -0,0 +1,387 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="5f8a3521-157f-4adb-a0ab-dc99bbf14aa0" name="Default" comment="small code refactor">
<change afterPath="$PROJECT_DIR$/.idea/description.html" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/project-template.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/out/production/Zadanie-03/com/tylkowski/crc/CrcTask.class" afterDir="false" />
<change afterPath="$PROJECT_DIR$/out/production/Zadanie-03/com/tylkowski/crc/Main.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/tylkowski/crc/CrcTask.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/tylkowski/crc/CrcTask.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/tylkowski/crc/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/tylkowski/crc/Main.java" afterDir="false" />
</list>
<ignored path="$PROJECT_DIR$/out/" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="TRACKING_ENABLED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file leaf-file-name="Main.java" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/com/tylkowski/crc/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="238">
<caret line="14" column="57" selection-start-line="14" selection-start-column="51" selection-end-line="14" selection-end-column="57" />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="CrcTask.java" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/com/tylkowski/crc/CrcTask.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="102">
<caret line="13" column="22" selection-start-line="13" selection-start-column="22" selection-end-line="13" selection-end-column="22" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="FindInProjectRecents">
<findStrings>
<find>System.out.println</find>
<find>for</find>
<find />
<find>System</find>
<find>ch</find>
<find>shot</find>
<find>gen_deg</find>
<find>char</find>
<find>swa</find>
<find>chunk</find>
<find>toBin</find>
<find>toBinaryStr</find>
</findStrings>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." />
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/src/com/tylkowski/crc/CrcTask.java" />
<option value="$PROJECT_DIR$/src/com/tylkowski/crc/Main.java" />
</list>
</option>
</component>
<component name="JsBuildToolGruntFileManager" detection-done="true" sorting="DEFINITION_ORDER" />
<component name="JsBuildToolPackageJson" detection-done="true" sorting="DEFINITION_ORDER" />
<component name="JsGulpfileManager">
<detection-done>true</detection-done>
<sorting>DEFINITION_ORDER</sorting>
</component>
<component name="ProjectFrameBounds" extendedState="6">
<option name="x" value="676" />
<option name="width" value="697" />
<option name="height" value="735" />
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectView">
<navigator proportions="" version="1">
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="Zadanie-03" type="b2602c69:ProjectViewProjectNode" />
<item name="Zadanie-03" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="Zadanie-03" type="b2602c69:ProjectViewProjectNode" />
<item name="Zadanie-03" type="462c0819:PsiDirectoryNode" />
<item name="src" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="Zadanie-03" type="b2602c69:ProjectViewProjectNode" />
<item name="Zadanie-03" type="462c0819:PsiDirectoryNode" />
<item name="src" type="462c0819:PsiDirectoryNode" />
<item name="crc" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
<pane id="AndroidView" />
<pane id="Scope" />
<pane id="PackagesPane" />
</panes>
</component>
<component name="PropertiesComponent">
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
<property name="extract.method.default.visibility" value="private" />
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager">
<configuration name="Main" type="Application" factoryName="Application" temporary="true">
<option name="MAIN_CLASS_NAME" value="com.tylkowski.crc.Main" />
<module name="Zadanie-03" />
<option name="PROGRAM_PARAMETERS" value="&quot;1&quot; &quot;a&quot;" />
<option name="VM_PARAMETERS" value="-Dfile.encoding=latin-1" />
<option name="WORKING_DIRECTORY" value="file://$PROJECT_DIR$" />
<RunnerSettings RunnerId="Run" />
<ConfigurationWrapper RunnerId="Run" />
</configuration>
<configuration default="true" type="Application" factoryName="Application">
<extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
<option name="MAIN_CLASS_NAME" />
<option name="VM_PARAMETERS" />
<option name="PROGRAM_PARAMETERS" />
<option name="WORKING_DIRECTORY" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="ENABLE_SWING_INSPECTOR" value="false" />
<option name="ENV_VARIABLES" />
<option name="PASS_PARENT_ENVS" value="true" />
<module name="" />
<envs />
<method />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.Main" />
</list>
</recent_temporary>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="5f8a3521-157f-4adb-a0ab-dc99bbf14aa0" name="Default" comment="" />
<created>1528990184795</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1528990184795</updated>
<workItem from="1528990186268" duration="4202000" />
<workItem from="1529055268255" duration="3475000" />
<workItem from="1529355696900" duration="6551000" />
<workItem from="1529407679397" duration="19000" />
<workItem from="1529409635196" duration="1437000" />
<workItem from="1529428699866" duration="20522000" />
<workItem from="1529482084631" duration="5516000" />
</task>
<task id="LOCAL-00001" summary="project started">
<created>1529359475020</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1529359475021</updated>
</task>
<task id="LOCAL-00002" summary="poly generating &amp; swapping">
<created>1529361951813</created>
<option name="number" value="00002" />
<option name="presentableId" value="LOCAL-00002" />
<option name="project" value="LOCAL" />
<updated>1529361951813</updated>
</task>
<task id="LOCAL-00003" summary="encode added!">
<created>1529447421427</created>
<option name="number" value="00003" />
<option name="presentableId" value="LOCAL-00003" />
<option name="project" value="LOCAL" />
<updated>1529447421427</updated>
</task>
<task id="LOCAL-00004" summary="decode added!">
<created>1529449567531</created>
<option name="number" value="00004" />
<option name="presentableId" value="LOCAL-00004" />
<option name="project" value="LOCAL" />
<updated>1529449567531</updated>
</task>
<task id="LOCAL-00005" summary="small code refactor">
<created>1529450003665</created>
<option name="number" value="00005" />
<option name="presentableId" value="LOCAL-00005" />
<option name="project" value="LOCAL" />
<updated>1529450003665</updated>
</task>
<option name="localTasksCounter" value="6" />
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="41722000" />
</component>
<component name="ToolWindowManager">
<frame x="-8" y="-8" width="1382" height="744" extended-state="6" />
<editor active="true" />
<layout>
<window_info anchor="right" id="Palette" order="3" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Messages" order="7" weight="0.32838285" />
<window_info anchor="right" id="Palette&#9;" order="3" />
<window_info id="Image Layers" order="2" />
<window_info anchor="right" id="Capture Analysis" order="3" />
<window_info anchor="bottom" id="Event Log" order="7" side_tool="true" />
<window_info anchor="right" id="Maven Projects" order="3" />
<window_info anchor="bottom" id="Database Changes" order="7" show_stripe_button="false" />
<window_info anchor="bottom" id="Version Control" order="7" />
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.43069306" />
<window_info anchor="bottom" id="Terminal" order="7" />
<window_info id="Capture Tool" order="2" />
<window_info id="Designer" order="2" />
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.16414523" />
<window_info anchor="right" id="Database" order="3" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info id="UI Designer" order="2" />
<window_info anchor="right" id="Theme Preview" order="3" />
<window_info id="Favorites" order="2" side_tool="true" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.39933994" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="right" id="Commander" order="0" weight="0.4" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Find" order="1" />
</layout>
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="VcsContentAnnotationSettings">
<option name="myLimit" value="2678400000" />
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="project started" />
<MESSAGE value="poly generating &amp; swapping" />
<MESSAGE value="encode added!" />
<MESSAGE value="decode added!" />
<MESSAGE value="small code refactor" />
<option name="LAST_COMMIT_MESSAGE" value="small code refactor" />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/src/com/tylkowski/crc/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="204">
<caret line="12" lean-forward="true" selection-start-line="12" selection-end-line="12" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/com/tylkowski/crc/CrcTask.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="663">
<caret line="39" column="9" lean-forward="true" selection-start-line="39" selection-start-column="9" selection-end-line="39" selection-end-column="9" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/com/tylkowski/crc/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="85">
<caret line="5" column="44" selection-start-line="5" selection-start-column="44" selection-end-line="5" selection-end-column="44" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/com/tylkowski/crc/CrcTask.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="663">
<caret line="39" lean-forward="true" selection-start-line="39" selection-end-line="39" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/com/tylkowski/crc/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="68">
<caret line="5" column="44" selection-start-line="5" selection-start-column="44" selection-end-line="5" selection-end-column="44" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/com/tylkowski/crc/CrcTask.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="561">
<caret line="33" column="16" selection-start-line="33" selection-start-column="16" selection-end-line="33" selection-end-column="16" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/com/tylkowski/crc/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="85">
<caret line="5" column="44" selection-start-line="5" selection-start-column="44" selection-end-line="5" selection-end-column="44" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/com/tylkowski/crc/CrcTask.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="561">
<caret line="33" column="16" selection-start-line="33" selection-start-column="16" selection-end-line="33" selection-end-column="16" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/com/tylkowski/crc/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="442">
<caret line="26" lean-forward="true" selection-start-line="26" selection-end-line="26" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/com/tylkowski/crc/CrcTask.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="221">
<caret line="13" column="9" lean-forward="true" selection-start-line="13" selection-start-column="9" selection-end-line="13" selection-end-column="9" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/com/tylkowski/crc/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="272">
<caret line="16" column="69" selection-start-line="16" selection-start-column="69" selection-end-line="16" selection-end-column="69" />
</state>
</provider>
</entry>
<entry file="jar://C:/Program Files/Java/jdk1.8.0_77/src.zip!/java/lang/Integer.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="186">
<caret line="329" column="16" selection-start-line="329" selection-start-column="16" selection-end-line="329" selection-end-column="33" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/com/tylkowski/crc/CrcTask.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="102">
<caret line="13" column="22" selection-start-line="13" selection-start-column="22" selection-end-line="13" selection-end-column="22" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/com/tylkowski/crc/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="238">
<caret line="14" column="57" selection-start-line="14" selection-start-column="51" selection-end-line="14" selection-end-column="57" />
</state>
</provider>
</entry>
</component>
</project>

View File

@ -1,5 +1,7 @@
package com.tylkowski.crc;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.Arrays;
class CrcTask {
@ -8,23 +10,18 @@ class CrcTask {
private short[] polyGenerator;
private String rawMessage;
CrcTask(String message, String rawMessage) {
this.message = formatMessage(toBinary(message)) + "0000000000000000";
CrcTask(String message, String rawMessage) throws NoSuchFieldException, IllegalAccessException {
setEncoding();
this.message = formatMessage(toBinaryString(message)) + "0000000000000000";
this.rawMessage = rawMessage;
createGeneratingPolynomial();
}
private String toBinary(String s) {
byte[] bytes = s.getBytes();
StringBuilder binary = new StringBuilder();
for (byte b : bytes) {
int val = b;
for (int i = 0; i < 8; i++) {
binary.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
}
return binary.toString();
private void setEncoding() throws NoSuchFieldException, IllegalAccessException {
System.setProperty("file.encoding","ISO-8859-1");
Field charset = Charset.class.getDeclaredField("defaultCharset");
charset.setAccessible(true);
charset.set(null,null);
}
private String formatMessage(String message) {
@ -96,8 +93,8 @@ class CrcTask {
return Arrays.copyOfRange(remainder, firstNonZeroVal, remainder.length);
}
private short[] calcXOR(short[] chunk, short[] polyGenerator) {
int a = Integer.parseInt(shortArrayToBinaryString(chunk), 2);
private short[] calcXOR(short[] piece, short[] polyGenerator) {
int a = Integer.parseInt(shortArrayToBinaryString(piece), 2);
int b = Integer.parseInt(shortArrayToBinaryString(polyGenerator), 2);
String binaryString = Integer.toBinaryString(a ^ b);
return convertBinaryStringToShortArray(binaryString);
@ -111,35 +108,24 @@ class CrcTask {
return binaryString.toString();
}
private char getCharFromShortArray(short[] chunk, int from, int to) {
short[] sign = Arrays.copyOfRange(chunk, from, to);
private char getCharFromShortArray(short[] piece, int from, int to) {
short[] sign = Arrays.copyOfRange(piece, from, to);
int character = Integer.parseInt(shortArrayToBinaryString(sign), 2);
// System.out.println(character);
return (char) character;
}
private void fillPolynomial(short[] chunk) {
while (chunk.length % 8 != 0) {
chunk = addZeroAtBeginningOfArray(chunk);
private void fillPolynomial(short[] piece) {
while (piece.length % 8 != 0) {
piece = addZeroAtBeginningOfArray(piece);
}
}
private short[] addZeroAtBeginningOfArray(short[] chunk) {
short[] letterTemp = new short[chunk.length + 1];
letterTemp[0] = 0;
System.arraycopy(chunk, 0, letterTemp, 1, chunk.length);
return letterTemp;
}
private void convertMessageToBinaryShortArray() {
messageAsShortArray = new short[message.length()];
for (int i = 0; i < polyGenerator.length; i++) {
if (message.charAt(i) == 48) {
messageAsShortArray[i] = 0;
} else {
messageAsShortArray[i] = 1;
}
}
private short[] addZeroAtBeginningOfArray(short[] piece) {
short[] tempArray = new short[piece.length + 1];
tempArray[0] = 0;
System.arraycopy(piece, 0, tempArray, 1, piece.length);
return tempArray;
}
private short[] convertBinaryStringToShortArray(String binaryString) {
@ -173,18 +159,33 @@ class CrcTask {
}
String encode() {
convertMessageToBinaryShortArray();
messageAsShortArray = convertBinaryStringToShortArray(message);
messageAsShortArray = swapPolynomialValues(messageAsShortArray);
return rawMessage + generateFCS();
}
private String letterToBinaryString(char letter) {
// System.out.println("lt: " + letter + " " + (int) letter);
int a = (int) letter;
// if (a == 63) a = 157;
System.out.println("letter in int: " + a);
StringBuilder binaryString = new StringBuilder(Integer.toBinaryString(a));
while (binaryString.length() % 8 != 0) {
binaryString.insert(0, "0");
}
// System.out.println("bs" + binaryString.toString());
return binaryString.toString();
}
boolean decode(String encodedString) {
encodedString = toBinary(encodedString);
encodedString = toBinaryString(encodedString);
encodedString = fillPolyTo8(encodedString);
short[] encodedShortArray = convertBinaryStringToShortArray(encodedString);
encodedShortArray = swapPolynomialValues(encodedShortArray);
while (true) {
if (shortArrayContains(encodedShortArray, 1)) {
if (!shortArrayContains(encodedShortArray, 1)) {
return true;
}
@ -203,6 +204,15 @@ class CrcTask {
}
}
private String toBinaryString(String encodedString) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < encodedString.length(); i++) {
stringBuilder.append(letterToBinaryString(encodedString.charAt(i)));
}
return stringBuilder.toString();
}
private boolean shortArrayContains(short[] encodedShortArray, int value) {
for (short item : encodedShortArray) {
if (item == value) {

View File

@ -2,12 +2,19 @@ package com.tylkowski.crc;
public class Main {
public static void main(String[] args) {
// ex. in command line type "a"
CrcTask crcTask = new CrcTask(args[0], args[0]);
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
// use valid character encoding ISO-8859-1
// in command line type "1" for mode and "a" for message
//example "1" "a" -> this will encode string "a" and return FCS
// example "2" "abc" -> this will decode string "abc" and return true if it is valid or false if not
CrcTask crcTask = new CrcTask(args[1], args[1]);
if(args[0].equals("1")) {
//create FCS
System.out.println(crcTask.encode());
String input = crcTask.encode();
if (input.length() >= 3) System.out.println(crcTask.decode(input));
} else if (args[0].equals("2")) {
// check fcs
if (args[1].length() >= 3) System.out.println(crcTask.decode(args[1]));
}
}