summaryrefslogtreecommitdiff
path: root/android/VulpIRC/src/main/java/net/brokenfox/vulpirc/Util.java
diff options
context:
space:
mode:
authorTreeki <treeki@gmail.com>2014-02-18 02:54:06 +0100
committerTreeki <treeki@gmail.com>2014-02-18 02:54:06 +0100
commitd57224505be243c80e9dd64e00f0a7c3a1d841f0 (patch)
treefe2b7d5cc9b7ca993b50f526f73bcb8716c0a60e /android/VulpIRC/src/main/java/net/brokenfox/vulpirc/Util.java
parent05568c427eff856d3049d4a18a707f1b0b358bd2 (diff)
downloadbounce4-d57224505be243c80e9dd64e00f0a7c3a1d841f0.tar.gz
bounce4-d57224505be243c80e9dd64e00f0a7c3a1d841f0.zip
add Android client
Diffstat (limited to 'android/VulpIRC/src/main/java/net/brokenfox/vulpirc/Util.java')
-rw-r--r--android/VulpIRC/src/main/java/net/brokenfox/vulpirc/Util.java62
1 files changed, 62 insertions, 0 deletions
diff --git a/android/VulpIRC/src/main/java/net/brokenfox/vulpirc/Util.java b/android/VulpIRC/src/main/java/net/brokenfox/vulpirc/Util.java
new file mode 100644
index 0000000..c5dbff1
--- /dev/null
+++ b/android/VulpIRC/src/main/java/net/brokenfox/vulpirc/Util.java
@@ -0,0 +1,62 @@
+package net.brokenfox.vulpirc;
+
+import android.util.Log;
+
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CharsetEncoder;
+
+/**
+ * Created by ninji on 2/3/14.
+ */
+public class Util {
+
+ private static CharsetDecoder mUTF8Decoder = null;
+ private static CharsetEncoder mUTF8Encoder = null;
+
+ public static synchronized String readStringFromBuffer(ByteBuffer p) {
+ if (mUTF8Decoder == null)
+ mUTF8Decoder = Charset.forName("UTF-8").newDecoder();
+
+ int size = p.getInt();
+ if (size <= 0)
+ return "";
+
+ int beginPos = p.position();
+ int endPos = beginPos + size;
+ int saveLimit = p.limit();
+ p.limit(endPos);
+
+ String result = "";
+
+ try {
+ result = mUTF8Decoder.decode(p).toString();
+ } catch (CharacterCodingException e) {
+ Log.e("VulpIRC", "Utils.readStringFromBuffer caught decode exception:");
+ Log.e("VulpIRC", e.toString());
+ }
+
+ p.limit(saveLimit);
+ p.position(endPos);
+
+ return result;
+ }
+
+ public static synchronized byte[] encodeString(CharSequence s) {
+ if (mUTF8Encoder == null)
+ mUTF8Encoder = Charset.forName("UTF-8").newEncoder();
+
+ try {
+ return mUTF8Encoder.encode(CharBuffer.wrap(s)).array();
+ } catch (CharacterCodingException e) {
+ Log.e("VulpIRC", "Utils.encodeString caught encode exception:");
+ Log.e("VulpIRC", e.toString());
+
+ return new byte[0];
+ }
+ }
+
+}