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.

FallbackZipEncoding.java 3.0 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. /**
  23. * A fallback ZipEncoding, which uses a java.io means to encode names.
  24. *
  25. * <p>This implementation is not favorable for encodings other than
  26. * utf-8, because java.io encodes unmappable character as question
  27. * marks leading to unreadable ZIP entries on some operating
  28. * systems.</p>
  29. *
  30. * <p>Furthermore this implementation is unable to tell whether a
  31. * given name can be safely encoded or not.</p>
  32. *
  33. * <p>This implementation acts as a last resort implementation, when
  34. * neither {@link Simple8BitZipEncoding} nor {@link NioZipEncoding} is
  35. * available.</p>
  36. *
  37. * <p>The methods of this class are reentrant.</p>
  38. */
  39. class FallbackZipEncoding implements ZipEncoding {
  40. private final String charset;
  41. /**
  42. * Construct a fallback zip encoding, which uses the platform's
  43. * default charset.
  44. */
  45. public FallbackZipEncoding() {
  46. this.charset = null;
  47. }
  48. /**
  49. * Construct a fallback zip encoding, which uses the given charset.
  50. *
  51. * @param charset The name of the charset or {@code null} for
  52. * the platform's default character set.
  53. */
  54. public FallbackZipEncoding(final String charset) {
  55. this.charset = charset;
  56. }
  57. /**
  58. * @see org.apache.tools.zip.ZipEncoding#canEncode(java.lang.String)
  59. */
  60. public boolean canEncode(final String name) {
  61. return true;
  62. }
  63. /**
  64. * @see org.apache.tools.zip.ZipEncoding#encode(java.lang.String)
  65. */
  66. public ByteBuffer encode(final String name) throws IOException {
  67. if (this.charset == null) { // i.e. use default charset, see no-args constructor
  68. return ByteBuffer.wrap(name.getBytes());
  69. } else {
  70. return ByteBuffer.wrap(name.getBytes(this.charset));
  71. }
  72. }
  73. /**
  74. * @see org.apache.tools.zip.ZipEncoding#decode(byte[])
  75. */
  76. public String decode(final byte[] data) throws IOException {
  77. if (this.charset == null) { // i.e. use default charset, see no-args constructor
  78. return new String(data);
  79. } else {
  80. return new String(data, this.charset);
  81. }
  82. }
  83. }