Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new features user can choose output charset #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions .idea/codeStyles/Project.xml

This file was deleted.

19 changes: 0 additions & 19 deletions .idea/gradle.xml

This file was deleted.

38 changes: 0 additions & 38 deletions .idea/misc.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@ implementation 'com.github.hsmnzaydn:easy-csv:1.0.0'
EasyCsv easyCsv = new EasyCsv(MainActivity.this);

```
* to output the file in diffrent charset create EasyCsv object with below constructer
``` java

EasyCsv easyCsv = new EasyCsv(MainActivity.this, StandardCharsets.YOUR_CHOOISE);

```




**Step 2: Create your headerlist and datalist**

Expand Down Expand Up @@ -85,6 +91,10 @@ public void onFail(String err) {
});

```
# Changelog
* 3/5/2020
— you can output the file with chosen Charset like UTF-8 and UTF-16 etc ...

# License
<pre>
Copyright 2018 Huseyin Serkan Ozaydin
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ android {
compileSdkVersion 28
defaultConfig {
applicationId "net.ozaydin.serkan.easy_csv_creator"
minSdkVersion 15
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.ozaydin.serkan.easy_csv.FileCallback;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -21,7 +22,8 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

EasyCsv easyCsv = new EasyCsv(MainActivity.this);
EasyCsv easyCsv = new EasyCsv(MainActivity.this, StandardCharsets.UTF_16LE);
easyCsv = new EasyCsv(MainActivity.this, StandardCharsets.UTF_8); //with custom Charset outputfile

List<String> headerList = new ArrayList<>();
headerList.add("Name.Surname.Age.Adress.Location.Education-");
Expand Down
12 changes: 9 additions & 3 deletions easy_csv/src/main/java/net/ozaydin/serkan/easy_csv/EasyCsv.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
Expand All @@ -23,7 +23,7 @@ public class EasyCsv {
private Activity activity;
private File file;
private OutputStream outputStream;

private Charset outputCharset;
private String separatorColumn=",";
private String seperatorLine="\r\n";

Expand All @@ -32,7 +32,10 @@ public EasyCsv(Activity activity) {
this.activity = activity;
}


public EasyCsv(Activity activity, Charset outputCharset) {
this.activity = activity;
this.outputCharset = outputCharset;
}
/**
* It is used to create a csv file by processing the data received from the user.
* @param fileName Name of will create file
Expand Down Expand Up @@ -103,6 +106,9 @@ public void onSubscribe(Disposable d) {
@Override
public void onNext(Object o) {
String dataWithLineBreak = (String) o;
if (outputCharset != null) {
dataWithLineBreak = Utils.convertCharset(outputCharset, dataWithLineBreak);
}
try {
finalFo.write(dataWithLineBreak.getBytes());
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package net.ozaydin.serkan.easy_csv.Utility;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -29,6 +32,20 @@ public static List<String> separatorReplace(String separatorColumnSign,String se
return strings;
}

public static String convertCharset(Charset outputCharset, String text) {
try {
Charset defaultCharset = Charset.defaultCharset();
ByteBuffer inputBuffer = ByteBuffer.wrap(text.getBytes());

CharBuffer data = defaultCharset.decode(inputBuffer);
ByteBuffer outputBuffer = outputCharset.encode(data);
byte[] outputData = outputBuffer.array();

return new String(outputData, outputCharset);

} catch (Exception e) {
throw new IllegalStateException(e);
}
}

}