Debian Codegraph @

src/main/java/org/apache/tools/zip/FallbackZipEncoding.java

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