Skip to content

Commit 07d173b

Browse files
cushonJavac Team
authored andcommitted
Use String(byte[], Charset) to create strings for zip entry names
Instead of `CharsetDecoder`, which is less well optimized. PiperOrigin-RevId: 837427006
1 parent 9e362e1 commit 07d173b

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

java/com/google/turbine/zip/Zip.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import java.nio.MappedByteBuffer;
3030
import java.nio.channels.FileChannel;
3131
import java.nio.channels.FileChannel.MapMode;
32-
import java.nio.charset.CharacterCodingException;
33-
import java.nio.charset.CharsetDecoder;
3432
import java.nio.file.Path;
3533
import java.nio.file.StandardOpenOption;
3634
import java.util.Iterator;
@@ -111,7 +109,6 @@ static class ZipIterator implements Iterator<Entry> {
111109
private final Path path;
112110
private int cdindex = 0;
113111
private final MappedByteBuffer cd;
114-
private final CharsetDecoder decoder = UTF_8.newDecoder();
115112

116113
ZipIterator(Path path, FileChannel chan, MappedByteBuffer cd) {
117114
this.path = path;
@@ -138,15 +135,15 @@ public Entry next() {
138135
}
139136

140137
public String string(ByteBuffer buf, int offset, int length) {
138+
// TODO: cushon - switch to FFM on JDK 22+
139+
// MemorySegment.copy(MemorySegment.ofBuffer(buf), JAVA_BYTE, offset, bytes, 0, length);
140+
// TODO: cushon - switch to MemorySegment#getString on JDK 27(?)+
141+
// MemorySegment.ofBuffer(buf).getString(offset, UTF_8, length);
141142
buf = buf.duplicate();
142143
buf.position(offset);
143-
buf.limit(offset + length);
144-
decoder.reset();
145-
try {
146-
return decoder.decode(buf).toString();
147-
} catch (CharacterCodingException e) {
148-
throw new IOError(e);
149-
}
144+
byte[] bytes = new byte[length];
145+
buf.get(bytes);
146+
return new String(bytes, UTF_8);
150147
}
151148
}
152149

0 commit comments

Comments
 (0)