From d57224505be243c80e9dd64e00f0a7c3a1d841f0 Mon Sep 17 00:00:00 2001 From: Treeki Date: Tue, 18 Feb 2014 02:54:06 +0100 Subject: add Android client --- .../src/main/java/net/brokenfox/vulpirc/Util.java | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 android/VulpIRC/src/main/java/net/brokenfox/vulpirc/Util.java (limited to 'android/VulpIRC/src/main/java/net/brokenfox/vulpirc/Util.java') 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]; + } + } + +} -- cgit v1.2.3