Skip to content

Commit

Permalink
* Allow using Charset to avoid UnsupportedEncodingException from…
Browse files Browse the repository at this point in the history
… `BytePointer` (pull #384)
  • Loading branch information
frankfliu authored Mar 26, 2020
1 parent edc2220 commit 57124c2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

* Prevent potential `NullPointerException` in `Loader.checkVersion()` ([pull #385](https://github.com/bytedeco/javacpp/pull/385))
* Allow using `Charset` to avoid `UnsupportedEncodingException` from `BytePointer` ([pull #384](https://github.com/bytedeco/javacpp/pull/384))
* Add static `Pointer.isNull(Pointer p)` helper method, for convenience
* Add `MoveAdapter` and corresponding `@StdMove` annotation to support objects that require `std::move` from C++11
* Always use `File.pathSeparator` when passing multiple paths via the `BUILD_PATH` environment variable
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/org/bytedeco/javacpp/BytePointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.io.UnsupportedEncodingException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import org.bytedeco.javacpp.annotation.Cast;
import org.bytedeco.javacpp.annotation.ValueGetter;
import org.bytedeco.javacpp.annotation.ValueSetter;
Expand All @@ -50,6 +52,18 @@ public BytePointer(String s, String charsetName)
this(s.getBytes(charsetName).length + 1);
putString(s, charsetName);
}
/**
* Allocates enough memory for the encoded string and actually encodes it
* in the given charset before copying it.
*
* @param s the String to encode and copy
* @param charset the charset in which the bytes are encoded
* @see #putString(String, Charset)
*/
public BytePointer(String s, Charset charset) {
this(s.getBytes(charset).length + 1);
putString(s, charset);
}
/**
* Allocates enough memory for the encoded string and actually encodes it
* in the platform's default charset before copying it.
Expand Down Expand Up @@ -152,6 +166,16 @@ public String getString(String charsetName)
throws UnsupportedEncodingException {
return new String(getStringBytes(), charsetName);
}
/**
* Decodes the native bytes assuming they are encoded in the given charset.
* Assumes a null-terminated string if {@code limit <= position}.
*
* @param charset the charset in which the bytes are encoded
* @return a String from the null-terminated string
*/
public String getString(Charset charset) {
return new String(getStringBytes(), charset);
}
/**
* Decodes the native bytes assuming they are encoded in the platform's default charset.
* Assumes a null-terminated string if {@code limit <= position}.
Expand Down Expand Up @@ -179,6 +203,21 @@ public BytePointer putString(String s, String charsetName)
byte[] bytes = s.getBytes(charsetName);
return put(bytes).put(bytes.length, (byte)0).limit(bytes.length);
}
/**
* Encodes the String into the given charset and copies it in native memory,
* including a terminating null byte.
* Sets the limit to just before the terminating null byte.
*
* @param s the String to encode and copy
* @param charset the charset in which to encode the bytes
* @return this
* @see String#getBytes(Charset)
* @see #put(byte[])
*/
public BytePointer putString(String s, Charset charset) {
byte[] bytes = s.getBytes(charset);
return put(bytes).put(bytes.length, (byte)0).limit(bytes.length);
}
/**
* Encodes the String into the platform's default charset and copies it in
* native memory, including a terminating null byte.
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/bytedeco/javacpp/PointerPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package org.bytedeco.javacpp;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

/**
* The peer class to native pointers and arrays of {@code void*}.
Expand Down Expand Up @@ -52,6 +53,16 @@ public class PointerPointer<P extends Pointer> extends Pointer {
public PointerPointer(String[] array, String charsetName) throws UnsupportedEncodingException {
this(array.length); putString(array, charsetName);
}
/**
* Allocates enough memory for the array of strings and copies it.
*
* @param array the array of strings to copy
* @param charset the charset in which the bytes are encoded
* @see #putString(String[], String)
*/
public PointerPointer(String[] array, Charset charset) {
this(array.length); putString(array, charset);
}
/**
* Allocates enough memory for the array and copies it.
*
Expand Down Expand Up @@ -162,6 +173,12 @@ public String getString(long i, String charsetName) throws UnsupportedEncodingEx
BytePointer p = (BytePointer)get((Class<P>)BytePointer.class, i);
return p != null ? p.getString(charsetName) : null;
}
/** @return {@code get(BytePointer.class, i).getString(charset)}
* @see BytePointer#getString(Charset) */
public String getString(long i, Charset charset) {
BytePointer p = (BytePointer)get((Class<P>)BytePointer.class, i);
return p != null ? p.getString(charset) : null;
}

/**
* Creates one by one a new {@link BytePointer} for each {@link String},
Expand Down Expand Up @@ -193,6 +210,21 @@ public PointerPointer<P> putString(String[] array, String charsetName) throws Un
}
return put(pointerArray);
}
/**
* Creates one by one a new {@link BytePointer} for each {@link String},
* and writes them into the native {@code void*} array.
*
* @param array the array of {@link String} to read from
* @param charset the charset in which the bytes are encoded
* @return this
*/
public PointerPointer<P> putString(String[] array, Charset charset) {
pointerArray = (P[])new BytePointer[array.length];
for (int i = 0; i < array.length; i++) {
pointerArray[i] = array[i] != null ? (P)new BytePointer(array[i], charset) : null;
}
return put(pointerArray);
}

/**
* Writes the Pointer values into the native {@code void*} array.
Expand Down

0 comments on commit 57124c2

Please sign in to comment.