/** * Grow a byte buffer, so it has a minimal capacity or at least * the double capacity of the original buffer * * @param b The original buffer. * @param newCapacity The minimal requested new capacity. * @return A byte buffer <code>r</code> with * <code>r.capacity() = max(b.capacity()*2,newCapacity)</code> and * all the data contained in <code>b</code> copied to the beginning * of <code>r</code>. * */ internal static java.nio.ByteBuffer growBuffer(java.nio.ByteBuffer b, int newCapacity) { b.limit(b.position()); b.rewind(); int c2 = b.capacity() * 2; java.nio.ByteBuffer on = java.nio.ByteBuffer.allocate(c2 < newCapacity ? newCapacity : c2); on.put(b); return(on); }
/** * @see * org.apache.commons.compress.archivers.zip.ZipEncoding#encode(java.lang.String) */ public java.nio.ByteBuffer encode(String name) { java.nio.charset.CharsetEncoder enc = this.charset.newEncoder(); enc.onMalformedInput(java.nio.charset.CodingErrorAction.REPORT); enc.onUnmappableCharacter(java.nio.charset.CodingErrorAction.REPORT); java.nio.CharBuffer cb = java.nio.CharBuffer.wrap(name); java.nio.ByteBuffer outJ = java.nio.ByteBuffer.allocate(name.length() + (name.length() + 1) / 2); while (cb.remaining() > 0) { java.nio.charset.CoderResult res = enc.encode(cb, outJ, true); if (res.isUnmappable() || res.isMalformed()) { // write the unmappable characters in utf-16 // pseudo-URL encoding style to ByteBuffer. if (res.length() * 6 > outJ.remaining()) { outJ = ZipEncodingHelper.growBuffer(outJ, outJ.position() + res.length() * 6); } for (int i = 0; i < res.length(); ++i) { ZipEncodingHelper.appendSurrogate(outJ, cb.get()); } } else if (res.isOverflow()) { outJ = ZipEncodingHelper.growBuffer(outJ, 0); } else if (res.isUnderflow()) { enc.flush(outJ); break; } } outJ.limit(outJ.position()); outJ.rewind(); return(outJ); }
/** * @see * org.apache.commons.compress.archivers.zip.ZipEncoding#encode(java.lang.String) */ public java.nio.ByteBuffer encode(String name) { java.nio.ByteBuffer outJ = java.nio.ByteBuffer.allocate(name.length() + 6 + (name.length() + 1) / 2); for (int i = 0; i < name.length(); ++i) { char c = name.charAt(i); if (outJ.remaining() < 6) { outJ = ZipEncodingHelper.growBuffer(outJ, outJ.position() + 6); } if (!this.pushEncodedChar(outJ, c)) { ZipEncodingHelper.appendSurrogate(outJ, c); } } outJ.limit(outJ.position()); outJ.rewind(); return(outJ); }