summaryrefslogtreecommitdiff
path: root/morfologik-fsa/src/main/java/morfologik/util/BufferUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'morfologik-fsa/src/main/java/morfologik/util/BufferUtils.java')
-rw-r--r--morfologik-fsa/src/main/java/morfologik/util/BufferUtils.java54
1 files changed, 54 insertions, 0 deletions
diff --git a/morfologik-fsa/src/main/java/morfologik/util/BufferUtils.java b/morfologik-fsa/src/main/java/morfologik/util/BufferUtils.java
new file mode 100644
index 0000000..6ccfbc6
--- /dev/null
+++ b/morfologik-fsa/src/main/java/morfologik/util/BufferUtils.java
@@ -0,0 +1,54 @@
+package morfologik.util;
+
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+
+/**
+ * Utility functions for buffers.
+ */
+public final class BufferUtils {
+
+ /**
+ * No instances.
+ */
+ private BufferUtils() {
+ // empty
+ }
+
+ /**
+ * Ensure the byte buffer's capacity. If a new buffer is allocated, its
+ * content is empty (the old buffer's contents is not copied).
+ *
+ * @param buffer
+ * The buffer to check or <code>null</code> if a new buffer
+ * should be allocated.
+ */
+ public static ByteBuffer ensureCapacity(ByteBuffer buffer, int capacity) {
+ if (buffer == null || buffer.capacity() < capacity) {
+ buffer = ByteBuffer.allocate(capacity);
+ }
+ return buffer;
+ }
+
+ /**
+ * Ensure the char buffer's capacity. If a new buffer is allocated, its
+ * content is empty (the old buffer's contents is not copied).
+ *
+ * @param buffer
+ * The buffer to check or <code>null</code> if a new buffer
+ * should be allocated.
+ */
+ public static CharBuffer ensureCapacity(CharBuffer buffer, int capacity) {
+ if (buffer == null || buffer.capacity() < capacity) {
+ buffer = CharBuffer.allocate(capacity);
+ }
+ return buffer;
+ }
+
+ /**
+ * Convert a byte buffer to a string in platform default encoding.
+ */
+ public static String toString(ByteBuffer sequence) {
+ return new String(sequence.array(), sequence.position(), sequence.remaining());
+ }
+} \ No newline at end of file