-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix #30 #254
base: master
Are you sure you want to change the base?
fix #30 #254
Changes from 1 commit
a88b8a6
3fb7cec
b4ab181
d1658b9
61ce834
9d1d562
b796974
badc99e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
|
||
import java.nio.ByteBuffer; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
class DirectMemoryIO extends AbstractMemoryIO { | ||
static final com.kenai.jffi.MemoryIO IO = com.kenai.jffi.MemoryIO.getInstance(); | ||
|
@@ -186,8 +187,30 @@ public String getString(long offset) { | |
|
||
|
||
public String getString(long offset, int maxLength, Charset cs) { | ||
final byte[] bytes = IO.getZeroTerminatedByteArray(address() + offset, maxLength); | ||
return cs.decode(ByteBuffer.wrap(bytes)).toString(); | ||
if(cs == StandardCharsets.UTF_8){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this logic could apply to any Charset that has a minimum character width of 1 byte, correct? All such encodings should use a single-byte At the very least this should include the ISO-8859 encodings, which are all single-byte (their CharsetEncoder.maxBytesPerChar will all be 1.0). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agree that I should include other single byte terminator charsets, but CharsetEncoder.maxBytesPerChar won't be sufficient because it does not resolve to 1.0 for utf-8 |
||
final byte[] bytes = IO.getZeroTerminatedByteArray(address() + offset, maxLength); | ||
return cs.decode(ByteBuffer.wrap(bytes)).toString(); | ||
}else{ | ||
byte[] bytes = new byte[maxLength]; | ||
IO.getByteArray(address() + offset, bytes, 0, maxLength); | ||
final byte[] nullCharBytes = new String("\0").getBytes(cs); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could possibly be cached for common encodings (UTF-16, UTF-32) or it might be valid to just use a width of |
||
int nullIndex = maxLength; | ||
int matchingBytesCount = 0; | ||
for(int i = 0; i < (int)bytes.length; i++){ | ||
if(bytes[i] == nullCharBytes[matchingBytesCount]){ | ||
matchingBytesCount++; | ||
} else { | ||
matchingBytesCount = 0; | ||
} | ||
|
||
if(matchingBytesCount == nullCharBytes.length){ | ||
nullIndex = i; | ||
break; | ||
} | ||
} | ||
|
||
return new String(bytes, 0, nullIndex, cs); | ||
} | ||
} | ||
|
||
public void putString(long offset, String string, int maxLength, Charset cs) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please reformat according to more common Java formatting standards:
Otherwise we get a mix of formats and future patches will likely include unhelpful formatting updates.