-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #271 from fixrtm/fix-model-charset-detection
fix: all model files will be read as platform-native charset
- Loading branch information
Showing
2 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/main/java/com/anatawa12/fixRtm/ngtlib/renderer/model/PolygonModelCharsetDetector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.anatawa12.fixRtm.ngtlib.renderer.model | ||
|
||
import com.anatawa12.fixRtm.MS932 | ||
import jp.ngt.ngtlib.renderer.model.ModelFormatException | ||
import java.io.ByteArrayInputStream | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.io.SequenceInputStream | ||
import java.lang.ref.SoftReference | ||
import java.nio.charset.Charset | ||
import java.nio.charset.StandardCharsets | ||
|
||
object PolygonModelCharsetDetector { | ||
// allocating huge array makes GC many times so cache them | ||
// but the buffer will be not necessary after finish loading models, | ||
// so it should be weak/soft reference. | ||
private val buffer: ThreadLocal<SoftReference<ByteArray>> = ThreadLocal<SoftReference<ByteArray>>() | ||
|
||
// windows-31j: Shift_JIS with Microsoft Extension. Also known as Microsoft Code Page 932 | ||
private val tryingCharsets = arrayOf(StandardCharsets.UTF_8, MS932) | ||
|
||
private fun getBuffer(): ByteArray? { | ||
val ref: SoftReference<ByteArray>? = buffer.get() | ||
var bytes: ByteArray? = if (ref == null) null else ref.get() | ||
if (bytes == null) { | ||
// 4 Mi bytes | ||
buffer.set(SoftReference(ByteArray(1024 * 1024).also { bytes = it })) | ||
} | ||
return bytes | ||
} | ||
|
||
// TODO: test | ||
fun detectCharset(inputStream: InputStream): Pair<Charset, InputStream> { | ||
val buf = getBuffer() | ||
var c = 0 | ||
try { | ||
// read bytes fully to buf. | ||
var i: Int | ||
while (inputStream.read(buf, c, buf!!.size - c).also { i = it } != -1) { | ||
c += i | ||
if (c == buf.size) break | ||
} | ||
} catch (e: IOException) { | ||
throw ModelFormatException("On read file for charset detection", e) | ||
} | ||
// empty: any charset should return empty string so use default one | ||
if (c == 0) return Pair(Charset.defaultCharset(), inputStream) | ||
val returnInputStream: InputStream = SequenceInputStream(ByteArrayInputStream(buf, 0, c), inputStream) | ||
for (tryingCharset in tryingCharsets) { | ||
var s = String(buf, 0, c, tryingCharset) | ||
// trim last few chars to not make error for last bytes | ||
s = s.substring(0, s.length - 10) | ||
|
||
// No U+FFFD should mean no decoding error. | ||
if (s.indexOf('\ufffd') == -1) return Pair(tryingCharset, returnInputStream) | ||
} | ||
// no charsets are valid: use UTF8 | ||
return Pair(StandardCharsets.UTF_8, returnInputStream) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters