commit 3218c762c187c2ee26a0e4fece68b96cdde5d377 Author: Victor Date: Sat Aug 24 15:24:40 2013 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..41a705e --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.classpath +.cproject +.project +bin/ +gen/ +obj/ +tmp/ \ No newline at end of file diff --git a/AndroidManifest.xml b/AndroidManifest.xml new file mode 100644 index 0000000..4aa2c4f --- /dev/null +++ b/AndroidManifest.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/assets/webcams.txt b/assets/webcams.txt new file mode 100644 index 0000000..92ed601 --- /dev/null +++ b/assets/webcams.txt @@ -0,0 +1,70 @@ + + http://img1.videoprobki.com.ua/camimg/cam704-5.jpg + http://85.90.192.233/cams.php?pathkey=cam5 + http://img1.videoprobki.com.ua/camimg/cam710-5.jpg + http://img1.videoprobki.com.ua/camimg/cam705-5.jpg +. http://img1.videoprobki.com.ua/camimg/cam703-5.jpg +. . http://img1.videoprobki.com.ua/camimg/cam701-5.jpg + http://img1.videoprobki.com.ua/camimg/cam717-5.jpg +. http://img1.videoprobki.com.ua/camimg/cam716-5.jpg +. - http://img1.videoprobki.com.ua/camimg/cam715-5.jpg + http://img1.videoprobki.com.ua/camimg/cam904-5.jpg + + 1 http://www.doncity.com.ua/cam/image1.jpg + 2 http://www.doncity.com.ua/cam/image2.jpg + 3 http://www.doncity.com.ua/cam/image3.jpg + 4 http://www.doncity.com.ua/cam/image4.jpg + + 1 http://shakhtar.com/data/img/webcam/exc003.jpg + 2 http://shakhtar.com/data/img/webcam/exc004.jpg + 3 http://shakhtar.com/data/img/webcam/exc047.jpg + 4 http://shakhtar.com/data/img/webcam/exc048.jpg + 5 http://shakhtar.com/data/img/webcam/exc049.jpg + 6 http://shakhtar.com/data/img/webcam/southt5.jpg + + http://scs.com.ua/webcam/sgiaz1.jpg + + + http://www.parisrama.com/webcam9.jpg +** 1 http://users.otenet.gr/~vbb/snapshot.jpg + 2 http://www.liveweather.gr/camz.php + 1 http://180.131.126.151/onejpeg.cgi + 2 http://vso.aa0.netvolante.jp/record/current.jpg + 3 http://webcam.dice-k.jp/mjpg/video.mjpg + -- http://webcamftp.arona.org/webcam/webcam/LasVistas1.jpg + -- http://webcamftp.arona.org/webcam/webcam/LasGalletas1.jpg + + + +- 1 http://abclocal.go.com/three/kabc/webcam/web1-1.jpg +- 2 http://abclocal.go.com/three/kabc/webcam/web1-2.jpg +- 3 http://abclocal.go.com/three/kabc/webcam/web2-1.jpg +- 4 http://abclocal.go.com/three/kabc/webcam/web2-2.jpg +- 1 http://207.251.86.238/cctv377.jpg +- 2 http://207.251.86.238/cctv382.jpg +- 3 http://207.251.86.238/cctv19.jpg +- 4 http://207.251.86.238/cctv20.jpg +- 5 http://207.251.86.238/cctv387.jpg +- 6 http://207.251.86.238/cctv400.jpg +- 7 http://207.251.86.238/cctv403.jpg +- 8 http://207.251.86.238/cctv414.jpg +- 9 http://207.251.86.238/cctv426.jpg +- 1 http://207.251.86.238/cctv371.jpg +- 2 http://207.251.86.238/cctv374.jpg +- 3 http://207.251.86.238/cctv375.jpg +- 4 http://207.251.86.238/cctv386.jpg + + + http://common.gcstatic.com/u/webcam/Webcam73-roof +UK. Hotel Victoria 1 http://www.tiger24.com:8080/cam_1.jpg +UK. Hotel Victoria 2 http://www.tiger24.com:8080/cam_3.jpg +UK. Hotel Victoria 3 http://www.tiger24.com:8080/cam_5.jpg + + +. - http://img1.videoprobki.com.ua/camimg/cam603-5.jpg + http://img1.videoprobki.com.ua/camimg/cam615-5.jpg + http://img1.videoprobki.com.ua/camimg/cam616-5.jpg + http://img1.videoprobki.com.ua/camimg/cam601-5.jpg + http://85.90.192.233/cams.php?pathkey=cam4 + http://85.90.192.233/cams.php?pathkey=cam7 +. http://img1.videoprobki.com.ua/camimg/cam612-5.jpg \ No newline at end of file diff --git a/assets/windows-1251.cp b/assets/windows-1251.cp new file mode 100644 index 0000000..05f1c73 Binary files /dev/null and b/assets/windows-1251.cp differ diff --git a/donetsk.htm b/donetsk.htm new file mode 100644 index 0000000..199407b --- /dev/null +++ b/donetsk.htm @@ -0,0 +1,33 @@ + + + + Донецк + + + + + +
+ Золотое Кольцо + Технарь + Мирный +
+ + + +
+ ул. Артёма. Остановка + ул. Артёма + Крытый Рынок +
+ + + +
+ просп. Кировский + просп. Ильича - Макеевское шоссе + Макеевское шоссе +
+ + + \ No newline at end of file diff --git a/ic_launcher-web.png b/ic_launcher-web.png new file mode 100644 index 0000000..a64fd38 Binary files /dev/null and b/ic_launcher-web.png differ diff --git a/libs/android-support-v4.jar b/libs/android-support-v4.jar new file mode 100644 index 0000000..6080877 Binary files /dev/null and b/libs/android-support-v4.jar differ diff --git a/proguard-project.txt b/proguard-project.txt new file mode 100644 index 0000000..f2fe155 --- /dev/null +++ b/proguard-project.txt @@ -0,0 +1,20 @@ +# To enable ProGuard in your project, edit project.properties +# to define the proguard.config property as described in that file. +# +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in ${sdk.dir}/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the ProGuard +# include property in project.properties. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/project.properties b/project.properties new file mode 100644 index 0000000..ce39f2d --- /dev/null +++ b/project.properties @@ -0,0 +1,14 @@ +# This file is automatically generated by Android Tools. +# Do not modify this file -- YOUR CHANGES WILL BE ERASED! +# +# This file must be checked in Version Control Systems. +# +# To customize properties used by the Ant build system edit +# "ant.properties", and override values to adapt the script to your +# project structure. +# +# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): +#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt + +# Project target. +target=android-18 diff --git a/res/drawable-hdpi/ic_launcher.png b/res/drawable-hdpi/ic_launcher.png new file mode 100644 index 0000000..a568a6c Binary files /dev/null and b/res/drawable-hdpi/ic_launcher.png differ diff --git a/res/drawable-ldpi/ic_launcher.png b/res/drawable-ldpi/ic_launcher.png new file mode 100644 index 0000000..1c856e5 Binary files /dev/null and b/res/drawable-ldpi/ic_launcher.png differ diff --git a/res/drawable-mdpi/ic_launcher.png b/res/drawable-mdpi/ic_launcher.png new file mode 100644 index 0000000..4b5b340 Binary files /dev/null and b/res/drawable-mdpi/ic_launcher.png differ diff --git a/res/drawable-xhdpi/ic_launcher.png b/res/drawable-xhdpi/ic_launcher.png new file mode 100644 index 0000000..16c9c44 Binary files /dev/null and b/res/drawable-xhdpi/ic_launcher.png differ diff --git a/res/layout-land/main.xml b/res/layout-land/main.xml new file mode 100644 index 0000000..36f9f79 --- /dev/null +++ b/res/layout-land/main.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/res/layout-port/main.xml b/res/layout-port/main.xml new file mode 100644 index 0000000..b83a0f9 --- /dev/null +++ b/res/layout-port/main.xml @@ -0,0 +1,12 @@ + + + + + \ No newline at end of file diff --git a/res/layout-port/webcam_view.xml b/res/layout-port/webcam_view.xml new file mode 100644 index 0000000..e61a1e1 --- /dev/null +++ b/res/layout-port/webcam_view.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/res/menu/main.xml b/res/menu/main.xml new file mode 100644 index 0000000..adb191f --- /dev/null +++ b/res/menu/main.xml @@ -0,0 +1,21 @@ + + + + + + + + + \ No newline at end of file diff --git a/res/values-v11/styles.xml b/res/values-v11/styles.xml new file mode 100644 index 0000000..11bb5b7 --- /dev/null +++ b/res/values-v11/styles.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml new file mode 100644 index 0000000..f7edeb5 --- /dev/null +++ b/res/values/strings.xml @@ -0,0 +1,9 @@ + + + + Donetsk Webcam + Обновить + Автообновление + Сохранить + + \ No newline at end of file diff --git a/res/values/styles.xml b/res/values/styles.xml new file mode 100644 index 0000000..d3ffe13 --- /dev/null +++ b/res/values/styles.xml @@ -0,0 +1,20 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/com/annimon/donetskwebcam/ImageDownloadTask.java b/src/com/annimon/donetskwebcam/ImageDownloadTask.java new file mode 100644 index 0000000..5809176 --- /dev/null +++ b/src/com/annimon/donetskwebcam/ImageDownloadTask.java @@ -0,0 +1,46 @@ +package com.annimon.donetskwebcam; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.os.AsyncTask; +import android.util.Log; +import android.widget.ImageView; + +public class ImageDownloadTask extends AsyncTask { + + private ImageView imageView; + + public ImageDownloadTask(ImageView imageView) { + this.imageView = imageView; + } + + @Override + protected Bitmap doInBackground(String... params) { + try { + URL url = new URL(params[0]); + URLConnection urlConn = url.openConnection(); + + InputStream is = urlConn.getInputStream(); + final Bitmap bitmap = BitmapFactory.decodeStream(is); + if (is != null) is.close(); + return bitmap; + } catch (IOException ex) { + Log.d(getClass().getName(), ex.getMessage(), ex); + } + return null; + } + + @Override + protected void onPostExecute(Bitmap result) { + super.onPostExecute(result); + if (result != null) { + imageView.setImageBitmap(result); + } + } + +} diff --git a/src/com/annimon/donetskwebcam/MainActivity.java b/src/com/annimon/donetskwebcam/MainActivity.java new file mode 100644 index 0000000..196956d --- /dev/null +++ b/src/com/annimon/donetskwebcam/MainActivity.java @@ -0,0 +1,57 @@ +package com.annimon.donetskwebcam; + +import android.content.Intent; +import android.content.res.Configuration; +import android.os.Bundle; +import android.support.v4.app.FragmentActivity; +import android.view.Menu; + +public class MainActivity extends FragmentActivity implements WebcamChooserFragment.OnWebcamSelectedListener { + + private String lastUrl; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + if (savedInstanceState != null) { + lastUrl = savedInstanceState.getString("url"); + } + if (isLandscape() && lastUrl != null) { + onWebcamSelected(lastUrl); + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + if (isLandscape()) { + getMenuInflater().inflate(R.menu.main, menu); + } + return true; + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putString("url", lastUrl); + } + + @Override + public void onWebcamSelected(String url) { + lastUrl = url; + WebcamViewerFragment viewer = (WebcamViewerFragment) getSupportFragmentManager() + .findFragmentById(R.id.webcamViewerFragment); + + if ( (viewer != null) && (viewer.isInLayout()) ) { + viewer.setImageUrl(url); + } else { + Intent intent = new Intent(this, WebcamViewerActvity.class); + intent.putExtra("url", url); + startActivity(intent); + } + } + + private boolean isLandscape() { + return (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE); + } +} diff --git a/src/com/annimon/donetskwebcam/Webcam.java b/src/com/annimon/donetskwebcam/Webcam.java new file mode 100644 index 0000000..cc9abee --- /dev/null +++ b/src/com/annimon/donetskwebcam/Webcam.java @@ -0,0 +1,20 @@ +package com.annimon.donetskwebcam; + +public class Webcam { + + private String name, url; + + public Webcam(String name, String url) { + this.name = name; + this.url = url; + } + + public String getName() { + return name; + } + + public String getUrl() { + return url; + } + +} diff --git a/src/com/annimon/donetskwebcam/WebcamAdapter.java b/src/com/annimon/donetskwebcam/WebcamAdapter.java new file mode 100644 index 0000000..5ac56ae --- /dev/null +++ b/src/com/annimon/donetskwebcam/WebcamAdapter.java @@ -0,0 +1,88 @@ +package com.annimon.donetskwebcam; + +import java.util.ArrayList; +import android.annotation.SuppressLint; +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.TextView; + + +public class WebcamAdapter extends BaseAdapter { + + private final LayoutInflater mInflater; + + private final ArrayList webcams; + + @SuppressLint("DefaultLocale") + public WebcamAdapter(Context context, String[] lineText) { + mInflater = LayoutInflater.from(context); + + webcams = new ArrayList(); + for (String line : lineText) { + if (line == null || line.length() <= 1) continue; + + String name, url; + + int tabIndex = line.indexOf('\t'); + if (tabIndex == -1) { + // Group title + name = line.trim().toUpperCase(); + url = null; + } else { + name = line.substring(0, tabIndex).trim(); + url = line.substring(tabIndex).trim(); + } + + webcams.add( new Webcam(name, url) ); + } + } + + @Override + public int getCount() { + return webcams.size(); + } + + @Override + public Object getItem(int position) { + return webcams.get(position); + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + boolean isGroupTitle = !isEnabled(position); + + final ViewHolder holder; + if (convertView == null) { + convertView = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false); + + holder = new ViewHolder(); + holder.text = (TextView) convertView.findViewById(android.R.id.text1); + convertView.setTag(holder); + } else { + holder = (ViewHolder) convertView.getTag(); + } + + String name = webcams.get(position).getName(); + holder.text.setTextSize(isGroupTitle ? 18 : 13); + holder.text.setText(name); + + return convertView; + } + + @Override + public boolean isEnabled(int position) { + return webcams.get(position).getUrl() != null; + } + + private static class ViewHolder { + TextView text; + } +} diff --git a/src/com/annimon/donetskwebcam/WebcamChooserFragment.java b/src/com/annimon/donetskwebcam/WebcamChooserFragment.java new file mode 100644 index 0000000..3af0556 --- /dev/null +++ b/src/com/annimon/donetskwebcam/WebcamChooserFragment.java @@ -0,0 +1,77 @@ +package com.annimon.donetskwebcam; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import android.app.Activity; +import android.os.Bundle; +import android.support.v4.app.ListFragment; +import android.view.View; +import android.widget.ListView; +import com.annimon.encodings.StringCoder; + +public class WebcamChooserFragment extends ListFragment { + + + private WebcamAdapter adapter; + + public interface OnWebcamSelectedListener { + public void onWebcamSelected(String url); + } + + private OnWebcamSelectedListener listener; + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + String text = getText("webcams.txt", "windows-1251"); + + adapter = new WebcamAdapter(getActivity(), text.split("\n")); + + setListAdapter(adapter); + getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); + } + + @Override + public void onListItemClick(ListView l, View v, int position, long id) { + String url = ((Webcam)adapter.getItem(position)).getUrl(); + listener.onWebcamSelected(url); + } + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + if (activity instanceof OnWebcamSelectedListener) { + listener = (OnWebcamSelectedListener) activity; + } else { + throw new ClassCastException(activity.toString() + " must implement OnWebcamSelectedListener"); + } + } + + @Override + public void onDetach() { + super.onDetach(); + listener = null; + } + + private String getText(String textpath, String encoding) { + String text; + try { + InputStream is = getActivity().getAssets().open(textpath); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + int oneByte; + while ((oneByte = is.read()) != -1) { + baos.write((byte) oneByte); + } + baos.flush(); + + text = StringCoder.decodeString(baos.toByteArray(), encoding); + is.close(); + } catch (IOException ex) { + text = "No data"; + } + return text; + } +} diff --git a/src/com/annimon/donetskwebcam/WebcamViewerActvity.java b/src/com/annimon/donetskwebcam/WebcamViewerActvity.java new file mode 100644 index 0000000..e417f00 --- /dev/null +++ b/src/com/annimon/donetskwebcam/WebcamViewerActvity.java @@ -0,0 +1,33 @@ +package com.annimon.donetskwebcam; + +import android.content.res.Configuration; +import android.os.Bundle; +import android.support.v4.app.FragmentActivity; +import android.view.Menu; + +public class WebcamViewerActvity extends FragmentActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { + finish(); + return; + } + setContentView(R.layout.webcam_view); + Bundle extras = getIntent().getExtras(); + if (extras != null) { + String url = extras.getString("url"); + WebcamViewerFragment viewer = (WebcamViewerFragment) getSupportFragmentManager() + .findFragmentById(R.id.webcamViewerFragment); + viewer.setImageUrl(url); + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.main, menu); + return true; + } +} diff --git a/src/com/annimon/donetskwebcam/WebcamViewerFragment.java b/src/com/annimon/donetskwebcam/WebcamViewerFragment.java new file mode 100644 index 0000000..3583d9a --- /dev/null +++ b/src/com/annimon/donetskwebcam/WebcamViewerFragment.java @@ -0,0 +1,102 @@ +package com.annimon.donetskwebcam; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Timer; +import java.util.TimerTask; +import android.graphics.Bitmap; +import android.graphics.drawable.BitmapDrawable; +import android.os.Bundle; +import android.os.Environment; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.Toast; + +public class WebcamViewerFragment extends Fragment { + + private String imageUrl; + private ImageView contentImage; + + private boolean autoUpdate; + private Timer timer; + + public WebcamViewerFragment() { + autoUpdate = false; + timer = new Timer(); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + setHasOptionsMenu(true); + contentImage = new ImageView(getActivity()); + return contentImage; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.menu_auto_update: + autoUpdate = !autoUpdate; + autoUpdate(); + break; + case R.id.menu_update: + update(); + break; + case R.id.menu_save: + try { + save("image" + File.separator + ""); + Toast.makeText(getActivity(), "", Toast.LENGTH_SHORT).show(); + } catch (IOException ex) { + ex.printStackTrace(); + } + break; + } + return true; + } + + public void setImageUrl(String url) { + imageUrl = url; + update(); + } + + public void update() { + new ImageDownloadTask(contentImage).execute(imageUrl); + } + + private void autoUpdate() { + TimerTask task = new TimerTask() { + + @Override + public void run() { + update(); + } + }; + if (autoUpdate) timer.schedule(task, 500, 100); + else { + timer.cancel(); + timer = new Timer(); + } + } + + public void save(String path) throws IOException { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss"); + String fullPath = Environment.getExternalStorageDirectory() + + File.separator + path + + File.separator + sdf.format(new Date()) + ".jpg"; + + OutputStream out = new FileOutputStream(new File(fullPath)); + Bitmap bitmap = ((BitmapDrawable)contentImage.getDrawable()).getBitmap(); + bitmap.compress(Bitmap.CompressFormat.JPEG, 85, out); + out.flush(); + out.close(); + } +} diff --git a/src/com/annimon/encodings/Charset.java b/src/com/annimon/encodings/Charset.java new file mode 100644 index 0000000..7c3bb08 --- /dev/null +++ b/src/com/annimon/encodings/Charset.java @@ -0,0 +1,44 @@ +package com.annimon.encodings; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; + +public class Charset { + + private char[] codepage; + private String charsetName; + + public Charset(String charsetName) { + codepage = new char[0x80]; + this.charsetName = charsetName; + } + + public void loadCodepage() throws UnsupportedEncodingException { + try { + codepage = CharsetLoader.getCodepage(charsetName); + } catch (IOException ioe) { + throw new UnsupportedEncodingException(); + } + } + + public final char decodeChar(byte signedByte) { + int unsigned = signedByte & 0xff; + + int charIndex = unsigned - 0x80; + if ( (0 <= charIndex) && (charIndex < codepage.length) ) { + return codepage[charIndex]; + } + + return (char) unsigned; + } + + public final byte encodeChar(char ch) { + if ( (0 <= ch) && (ch < 0x80) ) return (byte) ch; + + for (int i = 0; i < codepage.length; i++) { + if (ch == codepage[i]) return (byte) (i + 0x80); + } + + return (byte) ch; + } +} \ No newline at end of file diff --git a/src/com/annimon/encodings/CharsetLoader.java b/src/com/annimon/encodings/CharsetLoader.java new file mode 100644 index 0000000..62829f6 --- /dev/null +++ b/src/com/annimon/encodings/CharsetLoader.java @@ -0,0 +1,28 @@ +package com.annimon.encodings; + +import java.io.DataInputStream; +import java.io.IOException; +import android.content.res.AssetManager; + + +public class CharsetLoader { + + private static AssetManager assets; + + public static void setAssetManager(AssetManager assets) { + CharsetLoader.assets = assets; + } + + public static char[] getCodepage(String charsetName) throws IOException { + String assetPath = charsetName + ".cp"; + + DataInputStream dis = new DataInputStream( assets.open(assetPath) ); + char[] codepage = new char[0x80]; + for (int i = 0; i < codepage.length; i++) { + codepage[i] = dis.readChar(); + } + dis.close(); + + return codepage; + } +} diff --git a/src/com/annimon/encodings/StringCoder.java b/src/com/annimon/encodings/StringCoder.java new file mode 100644 index 0000000..ed9255d --- /dev/null +++ b/src/com/annimon/encodings/StringCoder.java @@ -0,0 +1,37 @@ +package com.annimon.encodings; + +import java.io.UnsupportedEncodingException; + +public class StringCoder { + + public static byte[] encodeString(String s, String enc) throws UnsupportedEncodingException { + byte[] bs; + try { + bs = s.getBytes(enc); + } catch (UnsupportedEncodingException x) { + Charset charset = new Charset(enc); + charset.loadCodepage(); + + bs = new byte[s.length()]; + for (int i = 0; i < s.length(); i++) { + bs[i] = charset.encodeChar(s.charAt(i)); + } + } + return bs; + } + + public static String decodeString(byte[] bs, String enc) throws UnsupportedEncodingException { + try { + return new String(bs, enc); + } catch (UnsupportedEncodingException x) { + Charset charset = new Charset(enc); + charset.loadCodepage(); + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < bs.length; i++) { + sb.append( charset.decodeChar(bs[i]) ); + } + return sb.toString(); + } + } +} \ No newline at end of file