/android/upstream/java/util/jar/Pack200.java

https://bitbucket.org/festevezga/xobotos · Java · 315 lines · 67 code · 44 blank · 204 comment · 0 complexity · 6617e4461f85cb9b755c9d110d01b7ef MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package java.util.jar;
  18. import java.beans.PropertyChangeListener;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import java.util.SortedMap;
  24. /**
  25. * Class factory for {@link Pack200.Packer} and {@link Pack200.Unpacker}.
  26. */
  27. public abstract class Pack200 {
  28. private static final String SYSTEM_PROPERTY_PACKER = "java.util.jar.Pack200.Packer";
  29. private static final String SYSTEM_PROPERTY_UNPACKER = "java.util.jar.Pack200.Unpacker";
  30. /**
  31. * Prevent this class from being instantiated.
  32. */
  33. private Pack200() {
  34. // do nothing
  35. }
  36. /**
  37. * Returns a new instance of a packer engine.
  38. * <p>
  39. * The implementation of the packer engine is defined by the system property
  40. * {@code 'java.util.jar.Pack200.Packer'}. If this system property is
  41. * defined an instance of the specified class is returned, otherwise the
  42. * system's default implementation is returned.
  43. *
  44. * @return an instance of {@code Packer}
  45. */
  46. public static Pack200.Packer newPacker() {
  47. String className = System.getProperty(SYSTEM_PROPERTY_PACKER, "org.apache.harmony.pack200.Pack200PackerAdapter");
  48. try {
  49. // TODO Not sure if this will cause problems with
  50. // loading the packer
  51. return (Packer) ClassLoader.getSystemClassLoader().loadClass(className).newInstance();
  52. } catch (Exception e) {
  53. throw new Error("Can't load class " + className, e);
  54. }
  55. }
  56. /**
  57. * Returns a new instance of a unpacker engine.
  58. * <p>
  59. * The implementation of the unpacker engine is defined by the system
  60. * property {@code 'java.util.jar.Pack200.Unpacker'}. If this system
  61. * property is defined an instance of the specified class is returned,
  62. * otherwise the system's default implementation is returned.
  63. *
  64. * @return a instance of {@code Unpacker}.
  65. */
  66. public static Pack200.Unpacker newUnpacker() {
  67. String className = System.getProperty(SYSTEM_PROPERTY_UNPACKER, "org.apache.harmony.unpack200.Pack200UnpackerAdapter");
  68. try {
  69. return (Unpacker) ClassLoader.getSystemClassLoader().loadClass(className).newInstance();
  70. } catch (Exception e) {
  71. throw new Error("Can't load class " + className, e);
  72. }
  73. }
  74. /**
  75. * The interface defining the API for converting a JAR file to an output
  76. * stream in the Pack200 format.
  77. */
  78. public static interface Packer {
  79. /**
  80. * the format of a class attribute name.
  81. */
  82. static final String CLASS_ATTRIBUTE_PFX = "pack.class.attribute.";
  83. /**
  84. * the format of a code attribute name.
  85. */
  86. static final String CODE_ATTRIBUTE_PFX = "pack.code.attribute.";
  87. /**
  88. * the deflation hint to set in the output archive.
  89. */
  90. static final String DEFLATE_HINT = "pack.deflate.hint";
  91. /**
  92. * the indicated amount of effort to use in compressing the archive.
  93. */
  94. static final String EFFORT = "pack.effort";
  95. /**
  96. * a String representation for {@code error}.
  97. */
  98. static final String ERROR = "error";
  99. /**
  100. * a String representation of {@code false}.
  101. */
  102. static final String FALSE = "false";
  103. /**
  104. * the format of a field attribute name.
  105. */
  106. static final String FIELD_ATTRIBUTE_PFX = "pack.field.attribute.";
  107. /**
  108. * a String representation for {@code keep}.
  109. */
  110. static final String KEEP = "keep";
  111. /**
  112. * decide if all elements shall transmit in their original order.
  113. */
  114. static final String KEEP_FILE_ORDER = "pack.keep.file.order";
  115. /**
  116. * a String representation for {@code latest}.
  117. */
  118. static final String LATEST = "latest";
  119. /**
  120. * the format of a method attribute name.
  121. */
  122. static final String METHOD_ATTRIBUTE_PFX = "pack.method.attribute.";
  123. /**
  124. * if it shall attempt to determine the latest modification time if this
  125. * is set to {@code LATEST}.
  126. */
  127. static final String MODIFICATION_TIME = "pack.modification.time";
  128. /**
  129. * a String representation of {@code pass}.
  130. */
  131. static final String PASS = "pass";
  132. /**
  133. * the file that will not be compressed.
  134. */
  135. static final String PASS_FILE_PFX = "pack.pass.file.";
  136. /**
  137. * packer progress as a percentage.
  138. */
  139. static final String PROGRESS = "pack.progress";
  140. /**
  141. * The number of bytes of each archive segment.
  142. */
  143. static final String SEGMENT_LIMIT = "pack.segment.limit";
  144. /**
  145. * a String representation of {@code strip}.
  146. */
  147. static final String STRIP = "strip";
  148. /**
  149. * a String representation of {@code true}.
  150. */
  151. static final String TRUE = "true";
  152. /**
  153. * the action to take if an unknown attribute is encountered.
  154. */
  155. static final String UNKNOWN_ATTRIBUTE = "pack.unknown.attribute";
  156. /**
  157. * Returns a sorted map of the properties of this packer.
  158. *
  159. * @return the properties of the packer.
  160. */
  161. SortedMap<String, String> properties();
  162. /**
  163. * Pack the specified JAR file to the specified output stream.
  164. *
  165. * @param in
  166. * JAR file to be compressed.
  167. * @param out
  168. * stream of compressed data.
  169. * @throws IOException
  170. * if I/O exception occurs.
  171. */
  172. void pack(JarFile in, OutputStream out) throws IOException;
  173. /**
  174. * Pack the data from the specified jar input stream to the specified
  175. * output stream.
  176. *
  177. * @param in
  178. * stream of uncompressed JAR data.
  179. * @param out
  180. * stream of compressed data.
  181. * @throws IOException
  182. * if I/O exception occurs.
  183. */
  184. void pack(JarInputStream in, OutputStream out) throws IOException;
  185. /**
  186. * add a listener for PropertyChange events
  187. *
  188. * @param listener
  189. * the listener to listen if PropertyChange events occurs
  190. */
  191. void addPropertyChangeListener(PropertyChangeListener listener);
  192. /**
  193. * remove a listener
  194. *
  195. * @param listener
  196. * listener to remove
  197. */
  198. void removePropertyChangeListener(PropertyChangeListener listener);
  199. }
  200. /**
  201. * The interface defining the API for converting a packed stream in the
  202. * Pack200 format to a JAR file.
  203. */
  204. public static interface Unpacker {
  205. /**
  206. * The String indicating if the unpacker should ignore all transmitted
  207. * values,can be replaced by either {@code true} or {@code false}.
  208. */
  209. static final String DEFLATE_HINT = "unpack.deflate.hint";
  210. /**
  211. * a String representation of {@code false}.
  212. */
  213. static final String FALSE = "false";
  214. /**
  215. * a String representation of {@code keep}.
  216. */
  217. static final String KEEP = "keep";
  218. /**
  219. * the progress as a {@code percentage}.
  220. */
  221. static final String PROGRESS = "unpack.progress";
  222. /**
  223. * a String representation of {@code true}.
  224. */
  225. static final String TRUE = "true";
  226. /**
  227. * Returns a sorted map of the properties of this unpacker.
  228. *
  229. * @return the properties of unpacker.
  230. */
  231. SortedMap<String, String> properties();
  232. /**
  233. * Unpack the specified stream to the specified JAR output stream.
  234. *
  235. * @param in
  236. * stream to uncompressed.
  237. * @param out
  238. * JAR output stream of uncompressed data.
  239. * @throws IOException
  240. * if I/O exception occurs.
  241. */
  242. void unpack(InputStream in, JarOutputStream out) throws IOException;
  243. /**
  244. * Unpack the contents of the specified {@code File} to the specified
  245. * JAR output stream.
  246. *
  247. * @param in
  248. * file to be uncompressed.
  249. * @param out
  250. * JAR output stream of uncompressed data.
  251. * @throws IOException
  252. * if I/O exception occurs.
  253. */
  254. void unpack(File in, JarOutputStream out) throws IOException;
  255. /**
  256. * add a listener for {@code PropertyChange} events.
  257. *
  258. * @param listener
  259. * the listener to listen if {@code PropertyChange} events
  260. * occurs.
  261. */
  262. void addPropertyChangeListener(PropertyChangeListener listener);
  263. /**
  264. * remove a listener.
  265. *
  266. * @param listener
  267. * listener to remove.
  268. */
  269. void removePropertyChangeListener(PropertyChangeListener listener);
  270. }
  271. }