| 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 |
import java.nio.CharBuffer; |
| 25 |
import java.nio.charset.Charset; |
| 26 |
import java.nio.charset.CharsetEncoder; |
| 27 |
import java.nio.charset.CoderResult; |
| 28 |
import java.nio.charset.CodingErrorAction; |
| 29 |
|
| 30 |
/** |
| 31 |
* A ZipEncoding, which uses a java.nio {@link |
| 32 |
* java.nio.charset.Charset Charset} to encode names. |
| 33 |
* |
| 34 |
* <p>This implementation works for all cases under java-1.5 or |
| 35 |
* later. However, in java-1.4, some charsets don't have a java.nio |
| 36 |
* implementation, most notably the default ZIP encoding Cp437.</p> |
| 37 |
* |
| 38 |
* <p>The methods of this class are reentrant.</p> |
| 39 |
*/ |
| 40 |
class NioZipEncoding implements ZipEncoding { |
| 41 |
private final Charset charset; |
| 42 |
|
| 43 |
/** |
| 44 |
* Construct an NIO based zip encoding, which wraps the given |
| 45 |
* charset. |
| 46 |
* |
| 47 |
* @param charset The NIO charset to wrap. |
| 48 |
*/ |
| 49 |
public NioZipEncoding(Charset charset) { |
| 50 |
this.charset = charset; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @see |
| 55 |
* org.apache.tools.zip.ZipEncoding#canEncode(java.lang.String) |
| 56 |
*/ |
| 57 |
public boolean canEncode(String name) { |
| 58 |
CharsetEncoder enc = this.charset.newEncoder(); |
| 59 |
enc.onMalformedInput(CodingErrorAction.REPORT); |
| 60 |
enc.onUnmappableCharacter(CodingErrorAction.REPORT); |
| 61 |
|
| 62 |
return enc.canEncode(name); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @see |
| 67 |
* org.apache.tools.zip.ZipEncoding#encode(java.lang.String) |
| 68 |
*/ |
| 69 |
public ByteBuffer encode(String name) { |
| 70 |
CharsetEncoder enc = this.charset.newEncoder(); |
| 71 |
|
| 72 |
enc.onMalformedInput(CodingErrorAction.REPORT); |
| 73 |
enc.onUnmappableCharacter(CodingErrorAction.REPORT); |
| 74 |
|
| 75 |
CharBuffer cb = CharBuffer.wrap(name); |
| 76 |
ByteBuffer out = ByteBuffer.allocate(name.length() |
| 77 |
+ (name.length() + 1) / 2); |
| 78 |
|
| 79 |
while (cb.remaining() > 0) { |
| 80 |
CoderResult res = enc.encode(cb, out,true); |
| 81 |
|
| 82 |
if (res.isUnmappable() || res.isMalformed()) { |
| 83 |
|
| 84 |
// write the unmappable characters in utf-16 |
| 85 |
// pseudo-URL encoding style to ByteBuffer. |
| 86 |
if (res.length() * 6 > out.remaining()) { |
| 87 |
out = ZipEncodingHelper.growBuffer(out, out.position() |
| 88 |
+ res.length() * 6); |
| 89 |
} |
| 90 |
|
| 91 |
for (int i=0; i<res.length(); ++i) { |
| 92 |
ZipEncodingHelper.appendSurrogate(out,cb.get()); |
| 93 |
} |
| 94 |
|
| 95 |
} else if (res.isOverflow()) { |
| 96 |
|
| 97 |
out = ZipEncodingHelper.growBuffer(out, 0); |
| 98 |
|
| 99 |
} else if (res.isUnderflow()) { |
| 100 |
|
| 101 |
enc.flush(out); |
| 102 |
break; |
| 103 |
|
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
out.limit(out.position()); |
| 108 |
out.rewind(); |
| 109 |
return out; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @see |
| 114 |
* org.apache.tools.zip.ZipEncoding#decode(byte[]) |
| 115 |
*/ |
| 116 |
public String decode(byte[] data) throws IOException { |
| 117 |
return this.charset.newDecoder() |
| 118 |
.onMalformedInput(CodingErrorAction.REPORT) |
| 119 |
.onUnmappableCharacter(CodingErrorAction.REPORT) |
| 120 |
.decode(ByteBuffer.wrap(data)).toString(); |
| 121 |
} |
| 122 |
} |