You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

NioZipEncoding.java 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.apache.tools.zip;
  20. import java.io.IOException;
  21. import java.nio.ByteBuffer;
  22. import java.nio.CharBuffer;
  23. import java.nio.charset.Charset;
  24. import java.nio.charset.CharsetEncoder;
  25. import java.nio.charset.CoderResult;
  26. import java.nio.charset.CodingErrorAction;
  27. /**
  28. * A ZipEncoding, which uses a java.nio {@link
  29. * java.nio.charset.Charset Charset} to encode names.
  30. *
  31. * <p>This implementation works for all cases under java-1.5 or
  32. * later. However, in java-1.4, some charsets don't have a java.nio
  33. * implementation, most notably the default ZIP encoding Cp437.</p>
  34. *
  35. * <p>The methods of this class are reentrant.</p>
  36. */
  37. class NioZipEncoding implements ZipEncoding {
  38. private final Charset charset;
  39. /**
  40. * Construct an NIO based zip encoding, which wraps the given
  41. * charset.
  42. *
  43. * @param charset The NIO charset to wrap.
  44. */
  45. public NioZipEncoding(Charset charset) {
  46. this.charset = charset;
  47. }
  48. /**
  49. * @see
  50. * org.apache.tools.zip.ZipEncoding#canEncode(java.lang.String)
  51. */
  52. public boolean canEncode(String name) {
  53. CharsetEncoder enc = this.charset.newEncoder();
  54. enc.onMalformedInput(CodingErrorAction.REPORT);
  55. enc.onUnmappableCharacter(CodingErrorAction.REPORT);
  56. return enc.canEncode(name);
  57. }
  58. /**
  59. * @see
  60. * org.apache.tools.zip.ZipEncoding#encode(java.lang.String)
  61. */
  62. public ByteBuffer encode(String name) {
  63. CharsetEncoder enc = this.charset.newEncoder();
  64. enc.onMalformedInput(CodingErrorAction.REPORT);
  65. enc.onUnmappableCharacter(CodingErrorAction.REPORT);
  66. CharBuffer cb = CharBuffer.wrap(name);
  67. ByteBuffer out = ByteBuffer.allocate(name.length()
  68. + (name.length() + 1) / 2);
  69. while (cb.remaining() > 0) {
  70. CoderResult res = enc.encode(cb, out,true);
  71. if (res.isUnmappable() || res.isMalformed()) {
  72. // write the unmappable characters in utf-16
  73. // pseudo-URL encoding style to ByteBuffer.
  74. if (res.length() * 6 > out.remaining()) {
  75. out = ZipEncodingHelper.growBuffer(out, out.position()
  76. + res.length() * 6);
  77. }
  78. for (int i=0; i<res.length(); ++i) {
  79. ZipEncodingHelper.appendSurrogate(out,cb.get());
  80. }
  81. } else if (res.isOverflow()) {
  82. out = ZipEncodingHelper.growBuffer(out, 0);
  83. } else if (res.isUnderflow()) {
  84. enc.flush(out);
  85. break;
  86. }
  87. }
  88. out.limit(out.position());
  89. out.rewind();
  90. return out;
  91. }
  92. /**
  93. * @see
  94. * org.apache.tools.zip.ZipEncoding#decode(byte[])
  95. */
  96. public String decode(byte[] data) throws IOException {
  97. return this.charset.newDecoder()
  98. .onMalformedInput(CodingErrorAction.REPORT)
  99. .onUnmappableCharacter(CodingErrorAction.REPORT)
  100. .decode(ByteBuffer.wrap(data)).toString();
  101. }
  102. }