redcar 0.3.2dev → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (485) hide show
  1. data/CHANGES +49 -2
  2. data/README.md +2 -7
  3. data/ROADMAP.md +8 -9
  4. data/Rakefile +29 -44
  5. data/bin/redcar +11 -2
  6. data/lib/openssl/History.txt +75 -0
  7. data/lib/openssl/License.txt +30 -0
  8. data/lib/openssl/Manifest.txt +99 -0
  9. data/lib/openssl/README.txt +24 -0
  10. data/lib/openssl/Rakefile +71 -0
  11. data/lib/openssl/build.properties.SAMPLE +7 -0
  12. data/lib/openssl/build.xml +57 -0
  13. data/lib/openssl/lib/jopenssl/version.rb +5 -0
  14. data/lib/openssl/lib/openssl.rb +24 -0
  15. data/lib/openssl/lib/openssl/bn.rb +33 -0
  16. data/lib/openssl/lib/openssl/buffering.rb +239 -0
  17. data/lib/openssl/lib/openssl/cipher.rb +56 -0
  18. data/lib/openssl/lib/openssl/digest.rb +46 -0
  19. data/lib/openssl/lib/openssl/dummy.rb +34 -0
  20. data/lib/openssl/lib/openssl/dummyssl.rb +13 -0
  21. data/lib/openssl/lib/openssl/ssl.rb +135 -0
  22. data/lib/openssl/lib/openssl/x509.rb +154 -0
  23. data/lib/openssl/mocha/COPYING +3 -0
  24. data/lib/openssl/mocha/MIT-LICENSE +7 -0
  25. data/lib/openssl/mocha/README +35 -0
  26. data/lib/openssl/mocha/RELEASE +171 -0
  27. data/lib/openssl/mocha/Rakefile +147 -0
  28. data/lib/openssl/mocha/TODO +48 -0
  29. data/lib/openssl/mocha/examples/misc.rb +36 -0
  30. data/lib/openssl/mocha/examples/mocha.rb +26 -0
  31. data/lib/openssl/mocha/examples/stubba.rb +65 -0
  32. data/lib/openssl/mocha/init.rb +3 -0
  33. data/lib/openssl/mocha/lib/mocha.rb +19 -0
  34. data/lib/openssl/mocha/lib/mocha/any_instance_method.rb +35 -0
  35. data/lib/openssl/mocha/lib/mocha/auto_verify.rb +111 -0
  36. data/lib/openssl/mocha/lib/mocha/central.rb +35 -0
  37. data/lib/openssl/mocha/lib/mocha/class_method.rb +62 -0
  38. data/lib/openssl/mocha/lib/mocha/deprecation.rb +22 -0
  39. data/lib/openssl/mocha/lib/mocha/exception_raiser.rb +17 -0
  40. data/lib/openssl/mocha/lib/mocha/expectation.rb +382 -0
  41. data/lib/openssl/mocha/lib/mocha/expectation_error.rb +6 -0
  42. data/lib/openssl/mocha/lib/mocha/expectation_list.rb +45 -0
  43. data/lib/openssl/mocha/lib/mocha/infinite_range.rb +25 -0
  44. data/lib/openssl/mocha/lib/mocha/inspect.rb +39 -0
  45. data/lib/openssl/mocha/lib/mocha/instance_method.rb +8 -0
  46. data/lib/openssl/mocha/lib/mocha/is_a.rb +9 -0
  47. data/lib/openssl/mocha/lib/mocha/metaclass.rb +7 -0
  48. data/lib/openssl/mocha/lib/mocha/missing_expectation.rb +27 -0
  49. data/lib/openssl/mocha/lib/mocha/mock.rb +200 -0
  50. data/lib/openssl/mocha/lib/mocha/multiple_yields.rb +20 -0
  51. data/lib/openssl/mocha/lib/mocha/no_yields.rb +11 -0
  52. data/lib/openssl/mocha/lib/mocha/object.rb +110 -0
  53. data/lib/openssl/mocha/lib/mocha/parameter_matchers.rb +9 -0
  54. data/lib/openssl/mocha/lib/mocha/parameter_matchers/all_of.rb +39 -0
  55. data/lib/openssl/mocha/lib/mocha/parameter_matchers/any_of.rb +44 -0
  56. data/lib/openssl/mocha/lib/mocha/parameter_matchers/anything.rb +30 -0
  57. data/lib/openssl/mocha/lib/mocha/parameter_matchers/has_entry.rb +39 -0
  58. data/lib/openssl/mocha/lib/mocha/parameter_matchers/has_key.rb +39 -0
  59. data/lib/openssl/mocha/lib/mocha/parameter_matchers/has_value.rb +39 -0
  60. data/lib/openssl/mocha/lib/mocha/parameter_matchers/includes.rb +37 -0
  61. data/lib/openssl/mocha/lib/mocha/parameter_matchers/instance_of.rb +39 -0
  62. data/lib/openssl/mocha/lib/mocha/parameter_matchers/kind_of.rb +39 -0
  63. data/lib/openssl/mocha/lib/mocha/pretty_parameters.rb +28 -0
  64. data/lib/openssl/mocha/lib/mocha/return_values.rb +31 -0
  65. data/lib/openssl/mocha/lib/mocha/setup_and_teardown.rb +23 -0
  66. data/lib/openssl/mocha/lib/mocha/single_return_value.rb +24 -0
  67. data/lib/openssl/mocha/lib/mocha/single_yield.rb +18 -0
  68. data/lib/openssl/mocha/lib/mocha/standalone.rb +32 -0
  69. data/lib/openssl/mocha/lib/mocha/stub.rb +18 -0
  70. data/lib/openssl/mocha/lib/mocha/test_case_adapter.rb +49 -0
  71. data/lib/openssl/mocha/lib/mocha/yield_parameters.rb +31 -0
  72. data/lib/openssl/mocha/lib/mocha_standalone.rb +2 -0
  73. data/lib/openssl/mocha/lib/stubba.rb +2 -0
  74. data/lib/openssl/mocha/templates/html_with_google_analytics.rb +742 -0
  75. data/lib/openssl/mocha/test/acceptance/expected_invocation_count_acceptance_test.rb +187 -0
  76. data/lib/openssl/mocha/test/acceptance/mocha_acceptance_test.rb +98 -0
  77. data/lib/openssl/mocha/test/acceptance/mocked_methods_dispatch_acceptance_test.rb +71 -0
  78. data/lib/openssl/mocha/test/acceptance/parameter_matcher_acceptance_test.rb +63 -0
  79. data/lib/openssl/mocha/test/acceptance/standalone_acceptance_test.rb +131 -0
  80. data/lib/openssl/mocha/test/acceptance/stubba_acceptance_test.rb +102 -0
  81. data/lib/openssl/mocha/test/active_record_test_case.rb +36 -0
  82. data/lib/openssl/mocha/test/deprecation_disabler.rb +15 -0
  83. data/lib/openssl/mocha/test/execution_point.rb +34 -0
  84. data/lib/openssl/mocha/test/integration/mocha_test_result_integration_test.rb +105 -0
  85. data/lib/openssl/mocha/test/integration/stubba_integration_test.rb +89 -0
  86. data/lib/openssl/mocha/test/integration/stubba_test_result_integration_test.rb +85 -0
  87. data/lib/openssl/mocha/test/method_definer.rb +18 -0
  88. data/lib/openssl/mocha/test/test_helper.rb +12 -0
  89. data/lib/openssl/mocha/test/test_runner.rb +31 -0
  90. data/lib/openssl/mocha/test/unit/any_instance_method_test.rb +124 -0
  91. data/lib/openssl/mocha/test/unit/array_inspect_test.rb +16 -0
  92. data/lib/openssl/mocha/test/unit/auto_verify_test.rb +122 -0
  93. data/lib/openssl/mocha/test/unit/central_test.rb +124 -0
  94. data/lib/openssl/mocha/test/unit/class_method_test.rb +196 -0
  95. data/lib/openssl/mocha/test/unit/date_time_inspect_test.rb +21 -0
  96. data/lib/openssl/mocha/test/unit/expectation_list_test.rb +69 -0
  97. data/lib/openssl/mocha/test/unit/expectation_raiser_test.rb +28 -0
  98. data/lib/openssl/mocha/test/unit/expectation_test.rb +399 -0
  99. data/lib/openssl/mocha/test/unit/hash_inspect_test.rb +16 -0
  100. data/lib/openssl/mocha/test/unit/infinite_range_test.rb +53 -0
  101. data/lib/openssl/mocha/test/unit/metaclass_test.rb +22 -0
  102. data/lib/openssl/mocha/test/unit/missing_expectation_test.rb +45 -0
  103. data/lib/openssl/mocha/test/unit/mock_test.rb +316 -0
  104. data/lib/openssl/mocha/test/unit/multiple_yields_test.rb +18 -0
  105. data/lib/openssl/mocha/test/unit/no_yield_test.rb +18 -0
  106. data/lib/openssl/mocha/test/unit/object_inspect_test.rb +35 -0
  107. data/lib/openssl/mocha/test/unit/object_test.rb +165 -0
  108. data/lib/openssl/mocha/test/unit/parameter_matchers/all_of_test.rb +26 -0
  109. data/lib/openssl/mocha/test/unit/parameter_matchers/any_of_test.rb +26 -0
  110. data/lib/openssl/mocha/test/unit/parameter_matchers/anything_test.rb +21 -0
  111. data/lib/openssl/mocha/test/unit/parameter_matchers/has_entry_test.rb +25 -0
  112. data/lib/openssl/mocha/test/unit/parameter_matchers/has_key_test.rb +25 -0
  113. data/lib/openssl/mocha/test/unit/parameter_matchers/has_value_test.rb +25 -0
  114. data/lib/openssl/mocha/test/unit/parameter_matchers/includes_test.rb +25 -0
  115. data/lib/openssl/mocha/test/unit/parameter_matchers/instance_of_test.rb +25 -0
  116. data/lib/openssl/mocha/test/unit/parameter_matchers/kind_of_test.rb +25 -0
  117. data/lib/openssl/mocha/test/unit/parameter_matchers/stub_matcher.rb +22 -0
  118. data/lib/openssl/mocha/test/unit/pretty_parameters_test.rb +32 -0
  119. data/lib/openssl/mocha/test/unit/return_values_test.rb +63 -0
  120. data/lib/openssl/mocha/test/unit/setup_and_teardown_test.rb +76 -0
  121. data/lib/openssl/mocha/test/unit/single_return_value_test.rb +33 -0
  122. data/lib/openssl/mocha/test/unit/single_yield_test.rb +18 -0
  123. data/lib/openssl/mocha/test/unit/string_inspect_test.rb +11 -0
  124. data/lib/openssl/mocha/test/unit/stub_test.rb +24 -0
  125. data/lib/openssl/mocha/test/unit/yield_parameters_test.rb +93 -0
  126. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/FOLDERS +53 -0
  127. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/LICENSE +340 -0
  128. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/README +117 -0
  129. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/bin/coderay +77 -0
  130. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/bin/coderay_stylesheet +4 -0
  131. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay.rb +319 -0
  132. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/duo.rb +29 -0
  133. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoder.rb +173 -0
  134. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/_map.rb +8 -0
  135. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/count.rb +21 -0
  136. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/debug.rb +46 -0
  137. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/div.rb +20 -0
  138. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/html.rb +260 -0
  139. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/html/classes.rb +77 -0
  140. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/html/css.rb +65 -0
  141. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/html/numerization.rb +122 -0
  142. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/html/output.rb +195 -0
  143. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/null.rb +26 -0
  144. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/page.rb +21 -0
  145. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/span.rb +20 -0
  146. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/statistic.rb +81 -0
  147. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/text.rb +33 -0
  148. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/tokens.rb +44 -0
  149. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb +71 -0
  150. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/yaml.rb +22 -0
  151. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/helpers/file_type.rb +182 -0
  152. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/helpers/gzip_simple.rb +122 -0
  153. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/helpers/plugin.rb +326 -0
  154. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/helpers/word_list.rb +119 -0
  155. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/scanner.rb +238 -0
  156. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/scanners/_map.rb +15 -0
  157. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/scanners/c.rb +163 -0
  158. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/scanners/debug.rb +60 -0
  159. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/scanners/delphi.rb +149 -0
  160. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/scanners/html.rb +177 -0
  161. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/scanners/nitro_xhtml.rb +133 -0
  162. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/scanners/plaintext.rb +16 -0
  163. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/scanners/rhtml.rb +73 -0
  164. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/scanners/ruby.rb +367 -0
  165. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/scanners/ruby/patterns.rb +230 -0
  166. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/scanners/xml.rb +18 -0
  167. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/style.rb +20 -0
  168. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/styles/_map.rb +7 -0
  169. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/styles/cycnus.rb +125 -0
  170. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/styles/murphy.rb +119 -0
  171. data/lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/tokens.rb +368 -0
  172. data/lib/openssl/mocha/vendor/meta_project-0.4.15/CHANGES +295 -0
  173. data/lib/openssl/mocha/vendor/meta_project-0.4.15/MIT-LICENSE +21 -0
  174. data/lib/openssl/mocha/vendor/meta_project-0.4.15/README +129 -0
  175. data/lib/openssl/mocha/vendor/meta_project-0.4.15/Rakefile +152 -0
  176. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project.rb +19 -0
  177. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/core_ext/open_uri.rb +22 -0
  178. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/core_ext/pathname.rb +36 -0
  179. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/core_ext/string.rb +5 -0
  180. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/http/multipart.rb +32 -0
  181. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/patois.rb +1 -0
  182. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/patois/parser.rb +99 -0
  183. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/project.rb +4 -0
  184. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/project/base.rb +9 -0
  185. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/project/codehaus.rb +1 -0
  186. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/project/codehaus/codehaus_project_svn.rb +31 -0
  187. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/project/trac.rb +1 -0
  188. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/project/trac/trac_project.rb +54 -0
  189. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/project/xforge.rb +5 -0
  190. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/project/xforge/ruby_forge.rb +47 -0
  191. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/project/xforge/session.rb +177 -0
  192. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/project/xforge/source_forge.rb +50 -0
  193. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/project/xforge/xfile.rb +45 -0
  194. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/project/xforge/xforge_base.rb +85 -0
  195. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/project_analyzer.rb +36 -0
  196. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/release/freshmeat.rb +267 -0
  197. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/release/raa.rb +572 -0
  198. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/scm_web.rb +2 -0
  199. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/scm_web/browser.rb +112 -0
  200. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/scm_web/pathname.rb +89 -0
  201. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/tracker.rb +6 -0
  202. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/tracker/base.rb +24 -0
  203. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/tracker/digit_issues.rb +34 -0
  204. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/tracker/issue.rb +57 -0
  205. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/tracker/jira.rb +2 -0
  206. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/tracker/jira/jira_issues.rb +35 -0
  207. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/tracker/jira/jira_tracker.rb +149 -0
  208. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/tracker/trac.rb +1 -0
  209. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/tracker/trac/trac_tracker.rb +33 -0
  210. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/tracker/xforge.rb +3 -0
  211. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/tracker/xforge/ruby_forge_tracker.rb +17 -0
  212. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/tracker/xforge/source_forge_tracker.rb +17 -0
  213. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/tracker/xforge/xforge_tracker.rb +191 -0
  214. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/meta_project/version_parser.rb +52 -0
  215. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/rake/contrib/xforge.rb +3 -0
  216. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/rake/contrib/xforge/base.rb +64 -0
  217. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/rake/contrib/xforge/news_publisher.rb +97 -0
  218. data/lib/openssl/mocha/vendor/meta_project-0.4.15/lib/rake/contrib/xforge/release.rb +134 -0
  219. data/lib/openssl/nbproject/genfiles.properties +5 -0
  220. data/lib/openssl/nbproject/jdk.xml +157 -0
  221. data/lib/openssl/nbproject/nbjdk.properties +1 -0
  222. data/lib/openssl/nbproject/nbjdk.xml +16 -0
  223. data/lib/openssl/nbproject/project.xml +117 -0
  224. data/lib/openssl/src/java/JopensslService.java +41 -0
  225. data/lib/openssl/src/java/org/jruby/ext/openssl/ASN1.java +930 -0
  226. data/lib/openssl/src/java/org/jruby/ext/openssl/Attribute.java +132 -0
  227. data/lib/openssl/src/java/org/jruby/ext/openssl/BN.java +769 -0
  228. data/lib/openssl/src/java/org/jruby/ext/openssl/BouncyCastlePEMHandler.java +69 -0
  229. data/lib/openssl/src/java/org/jruby/ext/openssl/Callable.java +10 -0
  230. data/lib/openssl/src/java/org/jruby/ext/openssl/Cipher.java +661 -0
  231. data/lib/openssl/src/java/org/jruby/ext/openssl/CipherStrings.java +1827 -0
  232. data/lib/openssl/src/java/org/jruby/ext/openssl/Config.java +49 -0
  233. data/lib/openssl/src/java/org/jruby/ext/openssl/DefaultPEMHandler.java +44 -0
  234. data/lib/openssl/src/java/org/jruby/ext/openssl/Digest.java +207 -0
  235. data/lib/openssl/src/java/org/jruby/ext/openssl/HMAC.java +183 -0
  236. data/lib/openssl/src/java/org/jruby/ext/openssl/NetscapeSPKI.java +212 -0
  237. data/lib/openssl/src/java/org/jruby/ext/openssl/OpenSSLImpl.java +341 -0
  238. data/lib/openssl/src/java/org/jruby/ext/openssl/OpenSSLReal.java +113 -0
  239. data/lib/openssl/src/java/org/jruby/ext/openssl/PEMHandler.java +40 -0
  240. data/lib/openssl/src/java/org/jruby/ext/openssl/PKCS10CertificationRequestExt.java +159 -0
  241. data/lib/openssl/src/java/org/jruby/ext/openssl/PKCS7.java +642 -0
  242. data/lib/openssl/src/java/org/jruby/ext/openssl/PKey.java +154 -0
  243. data/lib/openssl/src/java/org/jruby/ext/openssl/PKeyDH.java +401 -0
  244. data/lib/openssl/src/java/org/jruby/ext/openssl/PKeyDSA.java +419 -0
  245. data/lib/openssl/src/java/org/jruby/ext/openssl/PKeyRSA.java +762 -0
  246. data/lib/openssl/src/java/org/jruby/ext/openssl/Random.java +103 -0
  247. data/lib/openssl/src/java/org/jruby/ext/openssl/Request.java +336 -0
  248. data/lib/openssl/src/java/org/jruby/ext/openssl/SSL.java +71 -0
  249. data/lib/openssl/src/java/org/jruby/ext/openssl/SSLContext.java +358 -0
  250. data/lib/openssl/src/java/org/jruby/ext/openssl/SSLSocket.java +561 -0
  251. data/lib/openssl/src/java/org/jruby/ext/openssl/SimpleSecretKey.java +53 -0
  252. data/lib/openssl/src/java/org/jruby/ext/openssl/Utils.java +60 -0
  253. data/lib/openssl/src/java/org/jruby/ext/openssl/X509.java +108 -0
  254. data/lib/openssl/src/java/org/jruby/ext/openssl/X509CRL.java +446 -0
  255. data/lib/openssl/src/java/org/jruby/ext/openssl/X509Cert.java +520 -0
  256. data/lib/openssl/src/java/org/jruby/ext/openssl/X509Extensions.java +751 -0
  257. data/lib/openssl/src/java/org/jruby/ext/openssl/X509Name.java +405 -0
  258. data/lib/openssl/src/java/org/jruby/ext/openssl/X509Revoked.java +111 -0
  259. data/lib/openssl/src/java/org/jruby/ext/openssl/X509Store.java +282 -0
  260. data/lib/openssl/src/java/org/jruby/ext/openssl/X509StoreCtx.java +206 -0
  261. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/ASN1Registry.java +5135 -0
  262. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/Attribute.java +79 -0
  263. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/BIO.java +345 -0
  264. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/BIOFilter.java +38 -0
  265. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/Base64BIOFilter.java +84 -0
  266. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/CipherBIOFilter.java +157 -0
  267. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/Digest.java +126 -0
  268. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/EVP.java +136 -0
  269. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/EncContent.java +178 -0
  270. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/Encrypt.java +77 -0
  271. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/Envelope.java +169 -0
  272. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/IssuerAndSerial.java +35 -0
  273. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/MemBIO.java +117 -0
  274. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/MessageDigestBIOFilter.java +76 -0
  275. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/Mime.java +245 -0
  276. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/MimeHeader.java +103 -0
  277. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/MimeParam.java +69 -0
  278. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/NotVerifiedPKCS7Exception.java +38 -0
  279. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/NullSinkBIO.java +52 -0
  280. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/PKCS7.java +1276 -0
  281. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/PKCS7Data.java +168 -0
  282. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/PKCS7DataData.java +88 -0
  283. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/PKCS7DataDigest.java +64 -0
  284. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/PKCS7DataEncrypted.java +61 -0
  285. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/PKCS7DataEnveloped.java +85 -0
  286. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/PKCS7DataSigned.java +127 -0
  287. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/PKCS7DataSignedAndEnveloped.java +91 -0
  288. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/PKCS7Exception.java +68 -0
  289. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/RecipInfo.java +244 -0
  290. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/SMIME.java +281 -0
  291. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/SignEnvelope.java +202 -0
  292. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/Signed.java +345 -0
  293. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/SignerInfoWithPkey.java +365 -0
  294. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/TypeDiscriminating.java +34 -0
  295. data/lib/openssl/src/java/org/jruby/ext/openssl/impl/utils/Base64.java +1809 -0
  296. data/lib/openssl/src/java/org/jruby/ext/openssl/util/Base64Coder.java +103 -0
  297. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/CRL.java +59 -0
  298. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/Certificate.java +57 -0
  299. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/CertificateFile.java +46 -0
  300. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/CertificateHashDir.java +46 -0
  301. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/Function0.java +43 -0
  302. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/Function1.java +43 -0
  303. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/Function2.java +43 -0
  304. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/Function3.java +43 -0
  305. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/Function4.java +43 -0
  306. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/Function5.java +43 -0
  307. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/Lookup.java +560 -0
  308. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/LookupMethod.java +84 -0
  309. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/Name.java +85 -0
  310. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/PEMInputOutput.java +1210 -0
  311. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/PKey.java +41 -0
  312. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/PolicyTree.java +36 -0
  313. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/Purpose.java +475 -0
  314. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/Store.java +377 -0
  315. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/StoreContext.java +1356 -0
  316. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/Trust.java +279 -0
  317. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/VerifyParameter.java +324 -0
  318. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/X509Aux.java +43 -0
  319. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/X509AuxCertificate.java +170 -0
  320. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/X509Error.java +70 -0
  321. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/X509Object.java +88 -0
  322. data/lib/openssl/src/java/org/jruby/ext/openssl/x509store/X509Utils.java +541 -0
  323. data/lib/openssl/test/cert_with_ec_pk.cer +27 -0
  324. data/lib/openssl/test/fixture/ca_path/72fa7371.0 +19 -0
  325. data/lib/openssl/test/fixture/ca_path/verisign.pem +19 -0
  326. data/lib/openssl/test/fixture/cacert.pem +23 -0
  327. data/lib/openssl/test/fixture/cert_localhost.pem +19 -0
  328. data/lib/openssl/test/fixture/common.pem +48 -0
  329. data/lib/openssl/test/fixture/localhost_keypair.pem +18 -0
  330. data/lib/openssl/test/fixture/max.pem +29 -0
  331. data/lib/openssl/test/fixture/purpose/b70a5bc1.0 +24 -0
  332. data/lib/openssl/test/fixture/purpose/ca/PASSWD_OF_CA_KEY_IS_1234 +0 -0
  333. data/lib/openssl/test/fixture/purpose/ca/ca_config.rb +37 -0
  334. data/lib/openssl/test/fixture/purpose/ca/cacert.pem +24 -0
  335. data/lib/openssl/test/fixture/purpose/ca/newcerts/2_cert.pem +19 -0
  336. data/lib/openssl/test/fixture/purpose/ca/newcerts/3_cert.pem +19 -0
  337. data/lib/openssl/test/fixture/purpose/ca/private/cakeypair.pem +30 -0
  338. data/lib/openssl/test/fixture/purpose/ca/serial +1 -0
  339. data/lib/openssl/test/fixture/purpose/cacert.pem +24 -0
  340. data/lib/openssl/test/fixture/purpose/scripts/gen_cert.rb +127 -0
  341. data/lib/openssl/test/fixture/purpose/scripts/gen_csr.rb +50 -0
  342. data/lib/openssl/test/fixture/purpose/scripts/init_ca.rb +66 -0
  343. data/lib/openssl/test/fixture/purpose/sslclient.pem +19 -0
  344. data/lib/openssl/test/fixture/purpose/sslclient/csr.pem +10 -0
  345. data/lib/openssl/test/fixture/purpose/sslclient/keypair.pem +15 -0
  346. data/lib/openssl/test/fixture/purpose/sslclient/sslclient.pem +19 -0
  347. data/lib/openssl/test/fixture/purpose/sslserver.pem +19 -0
  348. data/lib/openssl/test/fixture/purpose/sslserver/csr.pem +10 -0
  349. data/lib/openssl/test/fixture/purpose/sslserver/keypair.pem +15 -0
  350. data/lib/openssl/test/fixture/purpose/sslserver/sslserver.pem +19 -0
  351. data/lib/openssl/test/fixture/verisign.pem +19 -0
  352. data/lib/openssl/test/fixture/verisign_c3.pem +14 -0
  353. data/lib/openssl/test/openssl/ssl_server.rb +99 -0
  354. data/lib/openssl/test/openssl/test_asn1.rb +199 -0
  355. data/lib/openssl/test/openssl/test_cipher.rb +196 -0
  356. data/lib/openssl/test/openssl/test_digest.rb +88 -0
  357. data/lib/openssl/test/openssl/test_hmac.rb +44 -0
  358. data/lib/openssl/test/openssl/test_ns_spki.rb +69 -0
  359. data/lib/openssl/test/openssl/test_pair.rb +149 -0
  360. data/lib/openssl/test/openssl/test_pkcs7.rb +160 -0
  361. data/lib/openssl/test/openssl/test_pkey_rsa.rb +49 -0
  362. data/lib/openssl/test/openssl/test_ssl.rb +413 -0
  363. data/lib/openssl/test/openssl/test_x509cert.rb +236 -0
  364. data/lib/openssl/test/openssl/test_x509crl.rb +234 -0
  365. data/lib/openssl/test/openssl/test_x509ext.rb +95 -0
  366. data/lib/openssl/test/openssl/test_x509name.rb +281 -0
  367. data/lib/openssl/test/openssl/test_x509req.rb +178 -0
  368. data/lib/openssl/test/openssl/test_x509store.rb +245 -0
  369. data/lib/openssl/test/openssl/utils.rb +135 -0
  370. data/lib/openssl/test/pkcs7_mime_enveloped.message +19 -0
  371. data/lib/openssl/test/pkcs7_mime_signed.message +30 -0
  372. data/lib/openssl/test/pkcs7_multipart_signed.message +45 -0
  373. data/lib/openssl/test/ref/compile.rb +8 -0
  374. data/lib/openssl/test/ref/pkcs1 +0 -0
  375. data/lib/openssl/test/ref/pkcs1.c +21 -0
  376. data/lib/openssl/test/test_cipher.rb +95 -0
  377. data/lib/openssl/test/test_integration.rb +139 -0
  378. data/lib/openssl/test/test_java.rb +98 -0
  379. data/lib/openssl/test/test_java_attribute.rb +25 -0
  380. data/lib/openssl/test/test_java_bio.rb +42 -0
  381. data/lib/openssl/test/test_java_mime.rb +173 -0
  382. data/lib/openssl/test/test_java_pkcs7.rb +769 -0
  383. data/lib/openssl/test/test_java_smime.rb +177 -0
  384. data/lib/openssl/test/test_openssl.rb +34 -0
  385. data/lib/openssl/test/test_openssl_x509.rb +34 -0
  386. data/lib/openssl/test/test_parse_certificate.rb +20 -0
  387. data/lib/openssl/test/test_pkey.rb +46 -0
  388. data/lib/openssl/test/test_x509store.rb +155 -0
  389. data/lib/openssl/test/ut_eof.rb +128 -0
  390. data/lib/redcar.rb +134 -8
  391. data/lib/redcar/installer.rb +46 -11
  392. data/lib/redcar/runner.rb +2 -1
  393. data/lib/redcar/usage.rb +12 -5
  394. data/lib/redcar_quick_start.rb +37 -0
  395. data/plugins/application/features/support/env.rb +9 -1
  396. data/plugins/application/icons/redcar_icon_beta.png +0 -0
  397. data/plugins/application/lib/application.rb +113 -43
  398. data/plugins/application/lib/application/clipboard.rb +28 -4
  399. data/plugins/application/lib/application/command.rb +4 -12
  400. data/plugins/application/lib/application/command/executor.rb +2 -2
  401. data/plugins/application/lib/application/keymap.rb +39 -0
  402. data/plugins/application/lib/application/keymap/builder.rb +19 -0
  403. data/plugins/application/lib/application/speedbar.rb +33 -24
  404. data/plugins/application/lib/application/tab.rb +15 -0
  405. data/plugins/application/lib/application/window.rb +28 -5
  406. data/plugins/application/spec/application/clipboard_spec.rb +20 -1
  407. data/plugins/application/spec/application/command/executor_spec.rb +7 -2
  408. data/plugins/application/spec/application/command_spec.rb +0 -5
  409. data/plugins/application/spec/application/keymap/builder_spec.rb +17 -0
  410. data/plugins/application/spec/application/menu_spec.rb +30 -0
  411. data/plugins/application/spec/application/speedbar_spec.rb +30 -6
  412. data/plugins/application/spec/application/window_spec.rb +2 -2
  413. data/plugins/application/spec/spec_helper.rb +1 -1
  414. data/plugins/application_swt/lib/application_swt.rb +3 -0
  415. data/plugins/application_swt/lib/application_swt/dialogs/filter_list_dialog_controller.rb +10 -1
  416. data/plugins/application_swt/lib/application_swt/html_tab.rb +1 -0
  417. data/plugins/application_swt/lib/application_swt/menu.rb +4 -3
  418. data/plugins/application_swt/lib/application_swt/menu/binding_translator.rb +12 -4
  419. data/plugins/application_swt/lib/application_swt/speedbar.rb +105 -19
  420. data/plugins/application_swt/lib/application_swt/swt_wrapper.rb +2 -0
  421. data/plugins/application_swt/lib/application_swt/window.rb +55 -23
  422. data/plugins/application_swt/spec/spec_helper.rb +0 -1
  423. data/plugins/auto_completer/lib/auto_completer.rb +57 -3
  424. data/plugins/auto_indenter/lib/auto_indenter.rb +1 -1
  425. data/plugins/core/lib/core.rb +5 -3
  426. data/plugins/core/lib/core/interface.rb +73 -0
  427. data/plugins/core/lib/core/model.rb +2 -0
  428. data/plugins/core/lib/core/observable.rb +12 -1
  429. data/plugins/core/lib/core/observable_struct.rb +34 -0
  430. data/plugins/core/lib/core/plugin/storage.rb +16 -3
  431. data/plugins/core/lib/core/reentry_helpers.rb +13 -0
  432. data/plugins/core/spec/core/observable_spec.rb +13 -0
  433. data/plugins/core/spec/core/storage_spec.rb +18 -0
  434. data/plugins/core/spec/spec_helper.rb +1 -0
  435. data/plugins/edit_view/features/cut_and_paste.feature +4 -2
  436. data/plugins/edit_view/features/soft_tabs.feature +243 -0
  437. data/plugins/edit_view/features/step_definitions/editing_steps.rb +91 -2
  438. data/plugins/edit_view/features/step_definitions/tab_steps.rb +1 -1
  439. data/plugins/edit_view/features/tabs.feature +146 -0
  440. data/plugins/edit_view/lib/edit_view.rb +197 -8
  441. data/plugins/edit_view/lib/edit_view/actions/arrow_keys.rb +121 -0
  442. data/plugins/edit_view/lib/edit_view/actions/deletion.rb +30 -0
  443. data/plugins/edit_view/lib/edit_view/actions/tab.rb +66 -0
  444. data/plugins/edit_view/lib/edit_view/command.rb +0 -2
  445. data/plugins/edit_view/lib/edit_view/document.rb +62 -27
  446. data/plugins/edit_view/lib/edit_view/document/command.rb +11 -0
  447. data/plugins/edit_view/lib/edit_view/edit_tab.rb +2 -1
  448. data/plugins/edit_view/lib/edit_view/info_speedbar.rb +69 -0
  449. data/plugins/edit_view/lib/edit_view/tab_settings.rb +40 -0
  450. data/plugins/edit_view/spec/edit_view/document_spec.rb +26 -0
  451. data/plugins/edit_view/spec/spec_helper.rb +0 -1
  452. data/plugins/edit_view_swt/lib/edit_view_swt.rb +57 -69
  453. data/plugins/edit_view_swt/lib/edit_view_swt/document.rb +24 -3
  454. data/plugins/edit_view_swt/lib/edit_view_swt/edit_tab.rb +23 -6
  455. data/plugins/edit_view_swt/spec/spec_helper.rb +0 -1
  456. data/plugins/edit_view_swt/vendor/java-mateview.rb +19 -2
  457. data/plugins/encryption/encryption.rb +38 -0
  458. data/plugins/encryption/ezcrypto.rb +652 -0
  459. data/plugins/encryption/jarmor-1.1.jar +0 -0
  460. data/plugins/encryption/plugin.rb +9 -0
  461. data/plugins/execute_current_tab/lib/execute_current_tab.rb +56 -0
  462. data/plugins/execute_current_tab/plugin.rb +8 -0
  463. data/plugins/html_view/lib/html_view.rb +16 -11
  464. data/plugins/plugin_manager_ui/lib/plugin_manager_ui.rb +2 -1
  465. data/plugins/plugin_manager_ui/views/index.html.erb +22 -4
  466. data/plugins/project/lib/project.rb +140 -26
  467. data/plugins/project/lib/project/drb_service.rb +75 -0
  468. data/plugins/project/lib/project/file_mirror.rb +5 -5
  469. data/plugins/project/lib/project/find_file_dialog.rb +123 -32
  470. data/plugins/project/spec/project/file_mirror_spec.rb +10 -1
  471. data/plugins/project/spec/spec_helper.rb +0 -1
  472. data/plugins/redcar/plugin.rb +1 -1
  473. data/plugins/redcar/redcar.rb +204 -109
  474. data/plugins/redcar/spec/redcar/redcar_spec.rb +70 -0
  475. data/plugins/redcar/spec/spec_helper.rb +4 -0
  476. data/plugins/repl/lib/repl.rb +14 -7
  477. data/plugins/repl/lib/repl/internal_mirror.rb +43 -22
  478. data/plugins/repl/spec/repl/internal_mirror_spec.rb +34 -7
  479. data/plugins/repl/spec/spec_helper.rb +0 -1
  480. data/plugins/tree/spec/spec_helper.rb +0 -1
  481. data/plugins/tree_view_swt/lib/tree_view_swt.rb +1 -1
  482. data/textmate/Bundles/RedcarRepl.tmbundle/Syntaxes/InternalRuby.plist +2 -18
  483. data/textmate/Bundles/asciidoc.tmbundle/Syntaxes/AsciiDoc.tmLanguage +428 -0
  484. metadata +790 -371
  485. data/lib/redcar/boot.rb +0 -108
@@ -0,0 +1,34 @@
1
+ /***** BEGIN LICENSE BLOCK *****
2
+ * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3
+ *
4
+ * The contents of this file are subject to the Common Public
5
+ * License Version 1.0 (the "License"); you may not use this file
6
+ * except in compliance with the License. You may obtain a copy of
7
+ * the License at http://www.eclipse.org/legal/cpl-v10.html
8
+ *
9
+ * Software distributed under the License is distributed on an "AS
10
+ * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11
+ * implied. See the License for the specific language governing
12
+ * rights and limitations under the License.
13
+ *
14
+ * Copyright (C) 2008 Ola Bini <ola.bini@gmail.com>
15
+ *
16
+ * Alternatively, the contents of this file may be used under the terms of
17
+ * either of the GNU General Public License Version 2 or later (the "GPL"),
18
+ * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19
+ * in which case the provisions of the GPL or the LGPL are applicable instead
20
+ * of those above. If you wish to allow use of your version of this file only
21
+ * under the terms of either the GPL or the LGPL, and not to allow others to
22
+ * use your version of this file under the terms of the CPL, indicate your
23
+ * decision by deleting the provisions above and replace them with the notice
24
+ * and other provisions required by the GPL or the LGPL. If you do not delete
25
+ * the provisions above, a recipient may use your version of this file under
26
+ * the terms of any one of the CPL, the GPL or the LGPL.
27
+ ***** END LICENSE BLOCK *****/
28
+ package org.jruby.ext.openssl.impl;
29
+
30
+ /**
31
+ * @author <a href="mailto:ola.bini@gmail.com">Ola Bini</a>
32
+ */
33
+ public abstract class TypeDiscriminating {
34
+ }// TypeDiscriminating
@@ -0,0 +1,1809 @@
1
+ package org.jruby.ext.openssl.impl.utils;
2
+
3
+ /**
4
+ * <p>Encodes and decodes to and from Base64 notation.</p>
5
+ * <p>Homepage: <a href="http://iharder.net/base64">http://iharder.net/base64</a>.</p>
6
+ *
7
+ * <p>The <tt>options</tt> parameter, which appears in a few places, is used to pass
8
+ * several pieces of information to the encoder. In the "higher level" methods such as
9
+ * encodeBytes( bytes, options ) the options parameter can be used to indicate such
10
+ * things as first gzipping the bytes before encoding them, not inserting linefeeds
11
+ * (though that breaks strict Base64 compatibility), and encoding using the URL-safe
12
+ * and Ordered dialects.</p>
13
+ *
14
+ * <p>The constants defined in Base64 can be OR-ed together to combine options, so you
15
+ * might make a call like this:</p>
16
+ *
17
+ * <code>String encoded = Base64.encodeBytes( mybytes, Base64.GZIP | Base64.DONT_BREAK_LINES );</code>
18
+ *
19
+ * <p>to compress the data before encoding it and then making the output have no newline characters.</p>
20
+ *
21
+ *
22
+ * <p>
23
+ * Change Log:
24
+ * </p>
25
+ * <ul>
26
+ * <li>v2.2.2 - Fixed encodeFileToFile and decodeFileToFile to use the
27
+ * Base64.InputStream class to encode and decode on the fly which uses
28
+ * less memory than encoding/decoding an entire file into memory before writing.</li>
29
+ * <li>v2.2.1 - Fixed bug using URL_SAFE and ORDERED encodings. Fixed bug
30
+ * when using very small files (~< 40 bytes).</li>
31
+ * <li>v2.2 - Added some helper methods for encoding/decoding directly from
32
+ * one file to the next. Also added a main() method to support command line
33
+ * encoding/decoding from one file to the next. Also added these Base64 dialects:
34
+ * <ol>
35
+ * <li>The default is RFC3548 format.</li>
36
+ * <li>Calling Base64.setFormat(Base64.BASE64_FORMAT.URLSAFE_FORMAT) generates
37
+ * URL and file name friendly format as described in Section 4 of RFC3548.
38
+ * http://www.faqs.org/rfcs/rfc3548.html</li>
39
+ * <li>Calling Base64.setFormat(Base64.BASE64_FORMAT.ORDERED_FORMAT) generates
40
+ * URL and file name friendly format that preserves lexical ordering as described
41
+ * in http://www.faqs.org/qa/rfcc-1940.html</li>
42
+ * </ol>
43
+ * Special thanks to Jim Kellerman at <a href="http://www.powerset.com/">http://www.powerset.com/</a>
44
+ * for contributing the new Base64 dialects.
45
+ * </li>
46
+ *
47
+ * <li>v2.1 - Cleaned up javadoc comments and unused variables and methods. Added
48
+ * some convenience methods for reading and writing to and from files.</li>
49
+ * <li>v2.0.2 - Now specifies UTF-8 encoding in places where the code fails on systems
50
+ * with other encodings (like EBCDIC).</li>
51
+ * <li>v2.0.1 - Fixed an error when decoding a single byte, that is, when the
52
+ * encoded data was a single byte.</li>
53
+ * <li>v2.0 - I got rid of methods that used booleans to set options.
54
+ * Now everything is more consolidated and cleaner. The code now detects
55
+ * when data that's being decoded is gzip-compressed and will decompress it
56
+ * automatically. Generally things are cleaner. You'll probably have to
57
+ * change some method calls that you were making to support the new
58
+ * options format (<tt>int</tt>s that you "OR" together).</li>
59
+ * <li>v1.5.1 - Fixed bug when decompressing and decoding to a
60
+ * byte[] using <tt>decode( String s, boolean gzipCompressed )</tt>.
61
+ * Added the ability to "suspend" encoding in the Output Stream so
62
+ * you can turn on and off the encoding if you need to embed base64
63
+ * data in an otherwise "normal" stream (like an XML file).</li>
64
+ * <li>v1.5 - Output stream pases on flush() command but doesn't do anything itself.
65
+ * This helps when using GZIP streams.
66
+ * Added the ability to GZip-compress objects before encoding them.</li>
67
+ * <li>v1.4 - Added helper methods to read/write files.</li>
68
+ * <li>v1.3.6 - Fixed OutputStream.flush() so that 'position' is reset.</li>
69
+ * <li>v1.3.5 - Added flag to turn on and off line breaks. Fixed bug in input stream
70
+ * where last buffer being read, if not completely full, was not returned.</li>
71
+ * <li>v1.3.4 - Fixed when "improperly padded stream" error was thrown at the wrong time.</li>
72
+ * <li>v1.3.3 - Fixed I/O streams which were totally messed up.</li>
73
+ * </ul>
74
+ *
75
+ * <p>
76
+ * I am placing this code in the Public Domain. Do with it as you will.
77
+ * This software comes with no guarantees or warranties but with
78
+ * plenty of well-wishing instead!
79
+ * Please visit <a href="http://iharder.net/base64">http://iharder.net/base64</a>
80
+ * periodically to check for updates or to contribute improvements.
81
+ * </p>
82
+ *
83
+ * @author Robert Harder
84
+ * @author rob@iharder.net
85
+ * @version 2.2.2
86
+ */
87
+ public class Base64
88
+ {
89
+
90
+ /* ******** P U B L I C F I E L D S ******** */
91
+
92
+
93
+ /** No options specified. Value is zero. */
94
+ public final static int NO_OPTIONS = 0;
95
+
96
+ /** Specify encoding. */
97
+ public final static int ENCODE = 1;
98
+
99
+
100
+ /** Specify decoding. */
101
+ public final static int DECODE = 0;
102
+
103
+
104
+ /** Specify that data should be gzip-compressed. */
105
+ public final static int GZIP = 2;
106
+
107
+
108
+ /** Don't break lines when encoding (violates strict Base64 specification) */
109
+ public final static int DONT_BREAK_LINES = 8;
110
+
111
+ /**
112
+ * Encode using Base64-like encoding that is URL- and Filename-safe as described
113
+ * in Section 4 of RFC3548:
114
+ * <a href="http://www.faqs.org/rfcs/rfc3548.html">http://www.faqs.org/rfcs/rfc3548.html</a>.
115
+ * It is important to note that data encoded this way is <em>not</em> officially valid Base64,
116
+ * or at the very least should not be called Base64 without also specifying that is
117
+ * was encoded using the URL- and Filename-safe dialect.
118
+ */
119
+ public final static int URL_SAFE = 16;
120
+
121
+
122
+ /**
123
+ * Encode using the special "ordered" dialect of Base64 described here:
124
+ * <a href="http://www.faqs.org/qa/rfcc-1940.html">http://www.faqs.org/qa/rfcc-1940.html</a>.
125
+ */
126
+ public final static int ORDERED = 32;
127
+
128
+
129
+ /* ******** P R I V A T E F I E L D S ******** */
130
+
131
+
132
+ /** Maximum line length (76) of Base64 output. */
133
+ private final static int MAX_LINE_LENGTH = 76;
134
+
135
+
136
+ /** The equals sign (=) as a byte. */
137
+ private final static byte EQUALS_SIGN = (byte)'=';
138
+
139
+
140
+ /** The new line character (\n) as a byte. */
141
+ private final static byte NEW_LINE = (byte)'\n';
142
+
143
+
144
+ /** Preferred encoding. */
145
+ private final static String PREFERRED_ENCODING = "UTF-8";
146
+
147
+
148
+ // I think I end up not using the BAD_ENCODING indicator.
149
+ //private final static byte BAD_ENCODING = -9; // Indicates error in encoding
150
+ private final static byte WHITE_SPACE_ENC = -5; // Indicates white space in encoding
151
+ private final static byte EQUALS_SIGN_ENC = -1; // Indicates equals sign in encoding
152
+
153
+
154
+ /* ******** S T A N D A R D B A S E 6 4 A L P H A B E T ******** */
155
+
156
+ /** The 64 valid Base64 values. */
157
+ //private final static byte[] ALPHABET;
158
+ /* Host platform me be something funny like EBCDIC, so we hardcode these values. */
159
+ private final static byte[] _STANDARD_ALPHABET =
160
+ {
161
+ (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
162
+ (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N',
163
+ (byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U',
164
+ (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z',
165
+ (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g',
166
+ (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n',
167
+ (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u',
168
+ (byte)'v', (byte)'w', (byte)'x', (byte)'y', (byte)'z',
169
+ (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5',
170
+ (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'+', (byte)'/'
171
+ };
172
+
173
+
174
+ /**
175
+ * Translates a Base64 value to either its 6-bit reconstruction value
176
+ * or a negative number indicating some other meaning.
177
+ **/
178
+ private final static byte[] _STANDARD_DECODABET =
179
+ {
180
+ -9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 0 - 8
181
+ -5,-5, // Whitespace: Tab and Linefeed
182
+ -9,-9, // Decimal 11 - 12
183
+ -5, // Whitespace: Carriage Return
184
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 14 - 26
185
+ -9,-9,-9,-9,-9, // Decimal 27 - 31
186
+ -5, // Whitespace: Space
187
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 33 - 42
188
+ 62, // Plus sign at decimal 43
189
+ -9,-9,-9, // Decimal 44 - 46
190
+ 63, // Slash at decimal 47
191
+ 52,53,54,55,56,57,58,59,60,61, // Numbers zero through nine
192
+ -9,-9,-9, // Decimal 58 - 60
193
+ -1, // Equals sign at decimal 61
194
+ -9,-9,-9, // Decimal 62 - 64
195
+ 0,1,2,3,4,5,6,7,8,9,10,11,12,13, // Letters 'A' through 'N'
196
+ 14,15,16,17,18,19,20,21,22,23,24,25, // Letters 'O' through 'Z'
197
+ -9,-9,-9,-9,-9,-9, // Decimal 91 - 96
198
+ 26,27,28,29,30,31,32,33,34,35,36,37,38, // Letters 'a' through 'm'
199
+ 39,40,41,42,43,44,45,46,47,48,49,50,51, // Letters 'n' through 'z'
200
+ -9,-9,-9,-9 // Decimal 123 - 126
201
+ /*,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 127 - 139
202
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 140 - 152
203
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 153 - 165
204
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 166 - 178
205
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 179 - 191
206
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 192 - 204
207
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 205 - 217
208
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 218 - 230
209
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 231 - 243
210
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9 // Decimal 244 - 255 */
211
+ };
212
+
213
+
214
+ /* ******** U R L S A F E B A S E 6 4 A L P H A B E T ******** */
215
+
216
+ /**
217
+ * Used in the URL- and Filename-safe dialect described in Section 4 of RFC3548:
218
+ * <a href="http://www.faqs.org/rfcs/rfc3548.html">http://www.faqs.org/rfcs/rfc3548.html</a>.
219
+ * Notice that the last two bytes become "hyphen" and "underscore" instead of "plus" and "slash."
220
+ */
221
+ private final static byte[] _URL_SAFE_ALPHABET =
222
+ {
223
+ (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
224
+ (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N',
225
+ (byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U',
226
+ (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z',
227
+ (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g',
228
+ (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n',
229
+ (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u',
230
+ (byte)'v', (byte)'w', (byte)'x', (byte)'y', (byte)'z',
231
+ (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5',
232
+ (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'-', (byte)'_'
233
+ };
234
+
235
+ /**
236
+ * Used in decoding URL- and Filename-safe dialects of Base64.
237
+ */
238
+ private final static byte[] _URL_SAFE_DECODABET =
239
+ {
240
+ -9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 0 - 8
241
+ -5,-5, // Whitespace: Tab and Linefeed
242
+ -9,-9, // Decimal 11 - 12
243
+ -5, // Whitespace: Carriage Return
244
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 14 - 26
245
+ -9,-9,-9,-9,-9, // Decimal 27 - 31
246
+ -5, // Whitespace: Space
247
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 33 - 42
248
+ -9, // Plus sign at decimal 43
249
+ -9, // Decimal 44
250
+ 62, // Minus sign at decimal 45
251
+ -9, // Decimal 46
252
+ -9, // Slash at decimal 47
253
+ 52,53,54,55,56,57,58,59,60,61, // Numbers zero through nine
254
+ -9,-9,-9, // Decimal 58 - 60
255
+ -1, // Equals sign at decimal 61
256
+ -9,-9,-9, // Decimal 62 - 64
257
+ 0,1,2,3,4,5,6,7,8,9,10,11,12,13, // Letters 'A' through 'N'
258
+ 14,15,16,17,18,19,20,21,22,23,24,25, // Letters 'O' through 'Z'
259
+ -9,-9,-9,-9, // Decimal 91 - 94
260
+ 63, // Underscore at decimal 95
261
+ -9, // Decimal 96
262
+ 26,27,28,29,30,31,32,33,34,35,36,37,38, // Letters 'a' through 'm'
263
+ 39,40,41,42,43,44,45,46,47,48,49,50,51, // Letters 'n' through 'z'
264
+ -9,-9,-9,-9 // Decimal 123 - 126
265
+ /*,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 127 - 139
266
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 140 - 152
267
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 153 - 165
268
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 166 - 178
269
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 179 - 191
270
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 192 - 204
271
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 205 - 217
272
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 218 - 230
273
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 231 - 243
274
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9 // Decimal 244 - 255 */
275
+ };
276
+
277
+
278
+
279
+ /* ******** O R D E R E D B A S E 6 4 A L P H A B E T ******** */
280
+
281
+ /**
282
+ * I don't get the point of this technique, but it is described here:
283
+ * <a href="http://www.faqs.org/qa/rfcc-1940.html">http://www.faqs.org/qa/rfcc-1940.html</a>.
284
+ */
285
+ private final static byte[] _ORDERED_ALPHABET =
286
+ {
287
+ (byte)'-',
288
+ (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4',
289
+ (byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9',
290
+ (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
291
+ (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N',
292
+ (byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U',
293
+ (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z',
294
+ (byte)'_',
295
+ (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g',
296
+ (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n',
297
+ (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u',
298
+ (byte)'v', (byte)'w', (byte)'x', (byte)'y', (byte)'z'
299
+ };
300
+
301
+ /**
302
+ * Used in decoding the "ordered" dialect of Base64.
303
+ */
304
+ private final static byte[] _ORDERED_DECODABET =
305
+ {
306
+ -9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 0 - 8
307
+ -5,-5, // Whitespace: Tab and Linefeed
308
+ -9,-9, // Decimal 11 - 12
309
+ -5, // Whitespace: Carriage Return
310
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 14 - 26
311
+ -9,-9,-9,-9,-9, // Decimal 27 - 31
312
+ -5, // Whitespace: Space
313
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 33 - 42
314
+ -9, // Plus sign at decimal 43
315
+ -9, // Decimal 44
316
+ 0, // Minus sign at decimal 45
317
+ -9, // Decimal 46
318
+ -9, // Slash at decimal 47
319
+ 1,2,3,4,5,6,7,8,9,10, // Numbers zero through nine
320
+ -9,-9,-9, // Decimal 58 - 60
321
+ -1, // Equals sign at decimal 61
322
+ -9,-9,-9, // Decimal 62 - 64
323
+ 11,12,13,14,15,16,17,18,19,20,21,22,23, // Letters 'A' through 'M'
324
+ 24,25,26,27,28,29,30,31,32,33,34,35,36, // Letters 'N' through 'Z'
325
+ -9,-9,-9,-9, // Decimal 91 - 94
326
+ 37, // Underscore at decimal 95
327
+ -9, // Decimal 96
328
+ 38,39,40,41,42,43,44,45,46,47,48,49,50, // Letters 'a' through 'm'
329
+ 51,52,53,54,55,56,57,58,59,60,61,62,63, // Letters 'n' through 'z'
330
+ -9,-9,-9,-9 // Decimal 123 - 126
331
+ /*,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 127 - 139
332
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 140 - 152
333
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 153 - 165
334
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 166 - 178
335
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 179 - 191
336
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 192 - 204
337
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 205 - 217
338
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 218 - 230
339
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 231 - 243
340
+ -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9 // Decimal 244 - 255 */
341
+ };
342
+
343
+
344
+ /* ******** D E T E R M I N E W H I C H A L H A B E T ******** */
345
+
346
+
347
+ /**
348
+ * Returns one of the _SOMETHING_ALPHABET byte arrays depending on
349
+ * the options specified.
350
+ * It's possible, though silly, to specify ORDERED and URLSAFE
351
+ * in which case one of them will be picked, though there is
352
+ * no guarantee as to which one will be picked.
353
+ */
354
+ private final static byte[] getAlphabet( int options )
355
+ {
356
+ if( (options & URL_SAFE) == URL_SAFE ) return _URL_SAFE_ALPHABET;
357
+ else if( (options & ORDERED) == ORDERED ) return _ORDERED_ALPHABET;
358
+ else return _STANDARD_ALPHABET;
359
+
360
+ } // end getAlphabet
361
+
362
+
363
+ /**
364
+ * Returns one of the _SOMETHING_DECODABET byte arrays depending on
365
+ * the options specified.
366
+ * It's possible, though silly, to specify ORDERED and URL_SAFE
367
+ * in which case one of them will be picked, though there is
368
+ * no guarantee as to which one will be picked.
369
+ */
370
+ private final static byte[] getDecodabet( int options )
371
+ {
372
+ if( (options & URL_SAFE) == URL_SAFE ) return _URL_SAFE_DECODABET;
373
+ else if( (options & ORDERED) == ORDERED ) return _ORDERED_DECODABET;
374
+ else return _STANDARD_DECODABET;
375
+
376
+ } // end getAlphabet
377
+
378
+
379
+
380
+ /** Defeats instantiation. */
381
+ private Base64(){}
382
+
383
+
384
+ /**
385
+ * Encodes or decodes two files from the command line;
386
+ * <strong>feel free to delete this method (in fact you probably should)
387
+ * if you're embedding this code into a larger program.</strong>
388
+ */
389
+ public final static void main( String[] args )
390
+ {
391
+ if( args.length < 3 ){
392
+ usage("Not enough arguments.");
393
+ } // end if: args.length < 3
394
+ else {
395
+ String flag = args[0];
396
+ String infile = args[1];
397
+ String outfile = args[2];
398
+ if( flag.equals( "-e" ) ){
399
+ Base64.encodeFileToFile( infile, outfile );
400
+ } // end if: encode
401
+ else if( flag.equals( "-d" ) ) {
402
+ Base64.decodeFileToFile( infile, outfile );
403
+ } // end else if: decode
404
+ else {
405
+ usage( "Unknown flag: " + flag );
406
+ } // end else
407
+ } // end else
408
+ } // end main
409
+
410
+ /**
411
+ * Prints command line usage.
412
+ *
413
+ * @param msg A message to include with usage info.
414
+ */
415
+ private final static void usage( String msg )
416
+ {
417
+ System.err.println( msg );
418
+ System.err.println( "Usage: java Base64 -e|-d inputfile outputfile" );
419
+ } // end usage
420
+
421
+
422
+ /* ******** E N C O D I N G M E T H O D S ******** */
423
+
424
+
425
+ /**
426
+ * Encodes up to the first three bytes of array <var>threeBytes</var>
427
+ * and returns a four-byte array in Base64 notation.
428
+ * The actual number of significant bytes in your array is
429
+ * given by <var>numSigBytes</var>.
430
+ * The array <var>threeBytes</var> needs only be as big as
431
+ * <var>numSigBytes</var>.
432
+ * Code can reuse a byte array by passing a four-byte array as <var>b4</var>.
433
+ *
434
+ * @param b4 A reusable byte array to reduce array instantiation
435
+ * @param threeBytes the array to convert
436
+ * @param numSigBytes the number of significant bytes in your array
437
+ * @return four byte array in Base64 notation.
438
+ * @since 1.5.1
439
+ */
440
+ private static byte[] encode3to4( byte[] b4, byte[] threeBytes, int numSigBytes, int options )
441
+ {
442
+ encode3to4( threeBytes, 0, numSigBytes, b4, 0, options );
443
+ return b4;
444
+ } // end encode3to4
445
+
446
+
447
+ /**
448
+ * <p>Encodes up to three bytes of the array <var>source</var>
449
+ * and writes the resulting four Base64 bytes to <var>destination</var>.
450
+ * The source and destination arrays can be manipulated
451
+ * anywhere along their length by specifying
452
+ * <var>srcOffset</var> and <var>destOffset</var>.
453
+ * This method does not check to make sure your arrays
454
+ * are large enough to accomodate <var>srcOffset</var> + 3 for
455
+ * the <var>source</var> array or <var>destOffset</var> + 4 for
456
+ * the <var>destination</var> array.
457
+ * The actual number of significant bytes in your array is
458
+ * given by <var>numSigBytes</var>.</p>
459
+ * <p>This is the lowest level of the encoding methods with
460
+ * all possible parameters.</p>
461
+ *
462
+ * @param source the array to convert
463
+ * @param srcOffset the index where conversion begins
464
+ * @param numSigBytes the number of significant bytes in your array
465
+ * @param destination the array to hold the conversion
466
+ * @param destOffset the index where output will be put
467
+ * @return the <var>destination</var> array
468
+ * @since 1.3
469
+ */
470
+ private static byte[] encode3to4(
471
+ byte[] source, int srcOffset, int numSigBytes,
472
+ byte[] destination, int destOffset, int options )
473
+ {
474
+ byte[] ALPHABET = getAlphabet( options );
475
+
476
+ // 1 2 3
477
+ // 01234567890123456789012345678901 Bit position
478
+ // --------000000001111111122222222 Array position from threeBytes
479
+ // --------| || || || | Six bit groups to index ALPHABET
480
+ // >>18 >>12 >> 6 >> 0 Right shift necessary
481
+ // 0x3f 0x3f 0x3f Additional AND
482
+
483
+ // Create buffer with zero-padding if there are only one or two
484
+ // significant bytes passed in the array.
485
+ // We have to shift left 24 in order to flush out the 1's that appear
486
+ // when Java treats a value as negative that is cast from a byte to an int.
487
+ int inBuff = ( numSigBytes > 0 ? ((source[ srcOffset ] << 24) >>> 8) : 0 )
488
+ | ( numSigBytes > 1 ? ((source[ srcOffset + 1 ] << 24) >>> 16) : 0 )
489
+ | ( numSigBytes > 2 ? ((source[ srcOffset + 2 ] << 24) >>> 24) : 0 );
490
+
491
+ switch( numSigBytes )
492
+ {
493
+ case 3:
494
+ destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
495
+ destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
496
+ destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ];
497
+ destination[ destOffset + 3 ] = ALPHABET[ (inBuff ) & 0x3f ];
498
+ return destination;
499
+
500
+ case 2:
501
+ destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
502
+ destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
503
+ destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ];
504
+ destination[ destOffset + 3 ] = EQUALS_SIGN;
505
+ return destination;
506
+
507
+ case 1:
508
+ destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
509
+ destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
510
+ destination[ destOffset + 2 ] = EQUALS_SIGN;
511
+ destination[ destOffset + 3 ] = EQUALS_SIGN;
512
+ return destination;
513
+
514
+ default:
515
+ return destination;
516
+ } // end switch
517
+ } // end encode3to4
518
+
519
+
520
+
521
+ /**
522
+ * Serializes an object and returns the Base64-encoded
523
+ * version of that serialized object. If the object
524
+ * cannot be serialized or there is another error,
525
+ * the method will return <tt>null</tt>.
526
+ * The object is not GZip-compressed before being encoded.
527
+ *
528
+ * @param serializableObject The object to encode
529
+ * @return The Base64-encoded object
530
+ * @since 1.4
531
+ */
532
+ public static String encodeObject( java.io.Serializable serializableObject )
533
+ {
534
+ return encodeObject( serializableObject, NO_OPTIONS );
535
+ } // end encodeObject
536
+
537
+
538
+
539
+ /**
540
+ * Serializes an object and returns the Base64-encoded
541
+ * version of that serialized object. If the object
542
+ * cannot be serialized or there is another error,
543
+ * the method will return <tt>null</tt>.
544
+ * <p>
545
+ * Valid options:<pre>
546
+ * GZIP: gzip-compresses object before encoding it.
547
+ * DONT_BREAK_LINES: don't break lines at 76 characters
548
+ * <i>Note: Technically, this makes your encoding non-compliant.</i>
549
+ * </pre>
550
+ * <p>
551
+ * Example: <code>encodeObject( myObj, Base64.GZIP )</code> or
552
+ * <p>
553
+ * Example: <code>encodeObject( myObj, Base64.GZIP | Base64.DONT_BREAK_LINES )</code>
554
+ *
555
+ * @param serializableObject The object to encode
556
+ * @param options Specified options
557
+ * @return The Base64-encoded object
558
+ * @see Base64#GZIP
559
+ * @see Base64#DONT_BREAK_LINES
560
+ * @since 2.0
561
+ */
562
+ public static String encodeObject( java.io.Serializable serializableObject, int options )
563
+ {
564
+ // Streams
565
+ java.io.ByteArrayOutputStream baos = null;
566
+ java.io.OutputStream b64os = null;
567
+ java.io.ObjectOutputStream oos = null;
568
+ java.util.zip.GZIPOutputStream gzos = null;
569
+
570
+ // Isolate options
571
+ int gzip = (options & GZIP);
572
+ int dontBreakLines = (options & DONT_BREAK_LINES);
573
+
574
+ try
575
+ {
576
+ // ObjectOutputStream -> (GZIP) -> Base64 -> ByteArrayOutputStream
577
+ baos = new java.io.ByteArrayOutputStream();
578
+ b64os = new Base64.OutputStream( baos, ENCODE | options );
579
+
580
+ // GZip?
581
+ if( gzip == GZIP )
582
+ {
583
+ gzos = new java.util.zip.GZIPOutputStream( b64os );
584
+ oos = new java.io.ObjectOutputStream( gzos );
585
+ } // end if: gzip
586
+ else
587
+ oos = new java.io.ObjectOutputStream( b64os );
588
+
589
+ oos.writeObject( serializableObject );
590
+ } // end try
591
+ catch( java.io.IOException e )
592
+ {
593
+ e.printStackTrace();
594
+ return null;
595
+ } // end catch
596
+ finally
597
+ {
598
+ try{ oos.close(); } catch( Exception e ){}
599
+ try{ gzos.close(); } catch( Exception e ){}
600
+ try{ b64os.close(); } catch( Exception e ){}
601
+ try{ baos.close(); } catch( Exception e ){}
602
+ } // end finally
603
+
604
+ // Return value according to relevant encoding.
605
+ try
606
+ {
607
+ return new String( baos.toByteArray(), PREFERRED_ENCODING );
608
+ } // end try
609
+ catch (java.io.UnsupportedEncodingException uue)
610
+ {
611
+ return new String( baos.toByteArray() );
612
+ } // end catch
613
+
614
+ } // end encode
615
+
616
+
617
+
618
+ /**
619
+ * Encodes a byte array into Base64 notation.
620
+ * Does not GZip-compress data.
621
+ *
622
+ * @param source The data to convert
623
+ * @since 1.4
624
+ */
625
+ public static String encodeBytes( byte[] source )
626
+ {
627
+ return encodeBytes( source, 0, source.length, NO_OPTIONS );
628
+ } // end encodeBytes
629
+
630
+
631
+
632
+ /**
633
+ * Encodes a byte array into Base64 notation.
634
+ * <p>
635
+ * Valid options:<pre>
636
+ * GZIP: gzip-compresses object before encoding it.
637
+ * DONT_BREAK_LINES: don't break lines at 76 characters
638
+ * <i>Note: Technically, this makes your encoding non-compliant.</i>
639
+ * </pre>
640
+ * <p>
641
+ * Example: <code>encodeBytes( myData, Base64.GZIP )</code> or
642
+ * <p>
643
+ * Example: <code>encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )</code>
644
+ *
645
+ *
646
+ * @param source The data to convert
647
+ * @param options Specified options
648
+ * @see Base64#GZIP
649
+ * @see Base64#DONT_BREAK_LINES
650
+ * @since 2.0
651
+ */
652
+ public static String encodeBytes( byte[] source, int options )
653
+ {
654
+ return encodeBytes( source, 0, source.length, options );
655
+ } // end encodeBytes
656
+
657
+
658
+ /**
659
+ * Encodes a byte array into Base64 notation.
660
+ * Does not GZip-compress data.
661
+ *
662
+ * @param source The data to convert
663
+ * @param off Offset in array where conversion should begin
664
+ * @param len Length of data to convert
665
+ * @since 1.4
666
+ */
667
+ public static String encodeBytes( byte[] source, int off, int len )
668
+ {
669
+ return encodeBytes( source, off, len, NO_OPTIONS );
670
+ } // end encodeBytes
671
+
672
+
673
+
674
+ /**
675
+ * Encodes a byte array into Base64 notation.
676
+ * <p>
677
+ * Valid options:<pre>
678
+ * GZIP: gzip-compresses object before encoding it.
679
+ * DONT_BREAK_LINES: don't break lines at 76 characters
680
+ * <i>Note: Technically, this makes your encoding non-compliant.</i>
681
+ * </pre>
682
+ * <p>
683
+ * Example: <code>encodeBytes( myData, Base64.GZIP )</code> or
684
+ * <p>
685
+ * Example: <code>encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )</code>
686
+ *
687
+ *
688
+ * @param source The data to convert
689
+ * @param off Offset in array where conversion should begin
690
+ * @param len Length of data to convert
691
+ * @param options Specified options
692
+ * @param options alphabet type is pulled from this (standard, url-safe, ordered)
693
+ * @see Base64#GZIP
694
+ * @see Base64#DONT_BREAK_LINES
695
+ * @since 2.0
696
+ */
697
+ public static String encodeBytes( byte[] source, int off, int len, int options )
698
+ {
699
+ // Isolate options
700
+ int dontBreakLines = ( options & DONT_BREAK_LINES );
701
+ int gzip = ( options & GZIP );
702
+
703
+ // Compress?
704
+ if( gzip == GZIP )
705
+ {
706
+ java.io.ByteArrayOutputStream baos = null;
707
+ java.util.zip.GZIPOutputStream gzos = null;
708
+ Base64.OutputStream b64os = null;
709
+
710
+
711
+ try
712
+ {
713
+ // GZip -> Base64 -> ByteArray
714
+ baos = new java.io.ByteArrayOutputStream();
715
+ b64os = new Base64.OutputStream( baos, ENCODE | options );
716
+ gzos = new java.util.zip.GZIPOutputStream( b64os );
717
+
718
+ gzos.write( source, off, len );
719
+ gzos.close();
720
+ } // end try
721
+ catch( java.io.IOException e )
722
+ {
723
+ e.printStackTrace();
724
+ return null;
725
+ } // end catch
726
+ finally
727
+ {
728
+ try{ gzos.close(); } catch( Exception e ){}
729
+ try{ b64os.close(); } catch( Exception e ){}
730
+ try{ baos.close(); } catch( Exception e ){}
731
+ } // end finally
732
+
733
+ // Return value according to relevant encoding.
734
+ try
735
+ {
736
+ return new String( baos.toByteArray(), PREFERRED_ENCODING );
737
+ } // end try
738
+ catch (java.io.UnsupportedEncodingException uue)
739
+ {
740
+ return new String( baos.toByteArray() );
741
+ } // end catch
742
+ } // end if: compress
743
+
744
+ // Else, don't compress. Better not to use streams at all then.
745
+ else
746
+ {
747
+ // Convert option to boolean in way that code likes it.
748
+ boolean breakLines = dontBreakLines == 0;
749
+
750
+ int len43 = len * 4 / 3;
751
+ byte[] outBuff = new byte[ ( len43 ) // Main 4:3
752
+ + ( (len % 3) > 0 ? 4 : 0 ) // Account for padding
753
+ + (breakLines ? ( len43 / MAX_LINE_LENGTH ) : 0) ]; // New lines
754
+ int d = 0;
755
+ int e = 0;
756
+ int len2 = len - 2;
757
+ int lineLength = 0;
758
+ for( ; d < len2; d+=3, e+=4 )
759
+ {
760
+ encode3to4( source, d+off, 3, outBuff, e, options );
761
+
762
+ lineLength += 4;
763
+ if( breakLines && lineLength == MAX_LINE_LENGTH )
764
+ {
765
+ outBuff[e+4] = NEW_LINE;
766
+ e++;
767
+ lineLength = 0;
768
+ } // end if: end of line
769
+ } // en dfor: each piece of array
770
+
771
+ if( d < len )
772
+ {
773
+ encode3to4( source, d+off, len - d, outBuff, e, options );
774
+ e += 4;
775
+ } // end if: some padding needed
776
+
777
+
778
+ // Return value according to relevant encoding.
779
+ try
780
+ {
781
+ return new String( outBuff, 0, e, PREFERRED_ENCODING );
782
+ } // end try
783
+ catch (java.io.UnsupportedEncodingException uue)
784
+ {
785
+ return new String( outBuff, 0, e );
786
+ } // end catch
787
+
788
+ } // end else: don't compress
789
+
790
+ } // end encodeBytes
791
+
792
+
793
+
794
+
795
+
796
+ /* ******** D E C O D I N G M E T H O D S ******** */
797
+
798
+
799
+ /**
800
+ * Decodes four bytes from array <var>source</var>
801
+ * and writes the resulting bytes (up to three of them)
802
+ * to <var>destination</var>.
803
+ * The source and destination arrays can be manipulated
804
+ * anywhere along their length by specifying
805
+ * <var>srcOffset</var> and <var>destOffset</var>.
806
+ * This method does not check to make sure your arrays
807
+ * are large enough to accomodate <var>srcOffset</var> + 4 for
808
+ * the <var>source</var> array or <var>destOffset</var> + 3 for
809
+ * the <var>destination</var> array.
810
+ * This method returns the actual number of bytes that
811
+ * were converted from the Base64 encoding.
812
+ * <p>This is the lowest level of the decoding methods with
813
+ * all possible parameters.</p>
814
+ *
815
+ *
816
+ * @param source the array to convert
817
+ * @param srcOffset the index where conversion begins
818
+ * @param destination the array to hold the conversion
819
+ * @param destOffset the index where output will be put
820
+ * @param options alphabet type is pulled from this (standard, url-safe, ordered)
821
+ * @return the number of decoded bytes converted
822
+ * @since 1.3
823
+ */
824
+ private static int decode4to3( byte[] source, int srcOffset, byte[] destination, int destOffset, int options )
825
+ {
826
+ byte[] DECODABET = getDecodabet( options );
827
+
828
+ // Example: Dk==
829
+ if( source[ srcOffset + 2] == EQUALS_SIGN )
830
+ {
831
+ // Two ways to do the same thing. Don't know which way I like best.
832
+ //int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
833
+ // | ( ( DECODABET[ source[ srcOffset + 1] ] << 24 ) >>> 12 );
834
+ int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 )
835
+ | ( ( DECODABET[ source[ srcOffset + 1] ] & 0xFF ) << 12 );
836
+
837
+ destination[ destOffset ] = (byte)( outBuff >>> 16 );
838
+ return 1;
839
+ }
840
+
841
+ // Example: DkL=
842
+ else if( source[ srcOffset + 3 ] == EQUALS_SIGN )
843
+ {
844
+ // Two ways to do the same thing. Don't know which way I like best.
845
+ //int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
846
+ // | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 )
847
+ // | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 );
848
+ int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 )
849
+ | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 )
850
+ | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) << 6 );
851
+
852
+ destination[ destOffset ] = (byte)( outBuff >>> 16 );
853
+ destination[ destOffset + 1 ] = (byte)( outBuff >>> 8 );
854
+ return 2;
855
+ }
856
+
857
+ // Example: DkLE
858
+ else
859
+ {
860
+ try{
861
+ // Two ways to do the same thing. Don't know which way I like best.
862
+ //int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
863
+ // | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 )
864
+ // | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 )
865
+ // | ( ( DECODABET[ source[ srcOffset + 3 ] ] << 24 ) >>> 24 );
866
+ int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 )
867
+ | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 )
868
+ | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) << 6)
869
+ | ( ( DECODABET[ source[ srcOffset + 3 ] ] & 0xFF ) );
870
+
871
+
872
+ destination[ destOffset ] = (byte)( outBuff >> 16 );
873
+ destination[ destOffset + 1 ] = (byte)( outBuff >> 8 );
874
+ destination[ destOffset + 2 ] = (byte)( outBuff );
875
+
876
+ return 3;
877
+ }catch( Exception e){
878
+ System.out.println(""+source[srcOffset]+ ": " + ( DECODABET[ source[ srcOffset ] ] ) );
879
+ System.out.println(""+source[srcOffset+1]+ ": " + ( DECODABET[ source[ srcOffset + 1 ] ] ) );
880
+ System.out.println(""+source[srcOffset+2]+ ": " + ( DECODABET[ source[ srcOffset + 2 ] ] ) );
881
+ System.out.println(""+source[srcOffset+3]+ ": " + ( DECODABET[ source[ srcOffset + 3 ] ] ) );
882
+ return -1;
883
+ } // end catch
884
+ }
885
+ } // end decodeToBytes
886
+
887
+
888
+
889
+
890
+ /**
891
+ * Very low-level access to decoding ASCII characters in
892
+ * the form of a byte array. Does not support automatically
893
+ * gunzipping or any other "fancy" features.
894
+ *
895
+ * @param source The Base64 encoded data
896
+ * @param off The offset of where to begin decoding
897
+ * @param len The length of characters to decode
898
+ * @return decoded data
899
+ * @since 1.3
900
+ */
901
+ public static byte[] decode( byte[] source, int off, int len, int options )
902
+ {
903
+ byte[] DECODABET = getDecodabet( options );
904
+
905
+ int len34 = len * 3 / 4;
906
+ byte[] outBuff = new byte[ len34 ]; // Upper limit on size of output
907
+ int outBuffPosn = 0;
908
+
909
+ byte[] b4 = new byte[4];
910
+ int b4Posn = 0;
911
+ int i = 0;
912
+ byte sbiCrop = 0;
913
+ byte sbiDecode = 0;
914
+ for( i = off; i < off+len; i++ )
915
+ {
916
+ sbiCrop = (byte)(source[i] & 0x7f); // Only the low seven bits
917
+ sbiDecode = DECODABET[ sbiCrop ];
918
+
919
+ if( sbiDecode >= WHITE_SPACE_ENC ) // White space, Equals sign or better
920
+ {
921
+ if( sbiDecode >= EQUALS_SIGN_ENC )
922
+ {
923
+ b4[ b4Posn++ ] = sbiCrop;
924
+ if( b4Posn > 3 )
925
+ {
926
+ outBuffPosn += decode4to3( b4, 0, outBuff, outBuffPosn, options );
927
+ b4Posn = 0;
928
+
929
+ // If that was the equals sign, break out of 'for' loop
930
+ if( sbiCrop == EQUALS_SIGN )
931
+ break;
932
+ } // end if: quartet built
933
+
934
+ } // end if: equals sign or better
935
+
936
+ } // end if: white space, equals sign or better
937
+ else
938
+ {
939
+ System.err.println( "Bad Base64 input character at " + i + ": " + source[i] + "(decimal)" );
940
+ return null;
941
+ } // end else:
942
+ } // each input character
943
+
944
+ byte[] out = new byte[ outBuffPosn ];
945
+ System.arraycopy( outBuff, 0, out, 0, outBuffPosn );
946
+ return out;
947
+ } // end decode
948
+
949
+
950
+
951
+
952
+ /**
953
+ * Decodes data from Base64 notation, automatically
954
+ * detecting gzip-compressed data and decompressing it.
955
+ *
956
+ * @param s the string to decode
957
+ * @return the decoded data
958
+ * @since 1.4
959
+ */
960
+ public static byte[] decode( String s )
961
+ {
962
+ return decode( s, NO_OPTIONS );
963
+ }
964
+
965
+
966
+ /**
967
+ * Decodes data from Base64 notation, automatically
968
+ * detecting gzip-compressed data and decompressing it.
969
+ *
970
+ * @param s the string to decode
971
+ * @param options encode options such as URL_SAFE
972
+ * @return the decoded data
973
+ * @since 1.4
974
+ */
975
+ public static byte[] decode( String s, int options )
976
+ {
977
+ byte[] bytes;
978
+ try
979
+ {
980
+ bytes = s.getBytes( PREFERRED_ENCODING );
981
+ } // end try
982
+ catch( java.io.UnsupportedEncodingException uee )
983
+ {
984
+ bytes = s.getBytes();
985
+ } // end catch
986
+ //</change>
987
+
988
+ // Decode
989
+ bytes = decode( bytes, 0, bytes.length, options );
990
+
991
+
992
+ // Check to see if it's gzip-compressed
993
+ // GZIP Magic Two-Byte Number: 0x8b1f (35615)
994
+ if( bytes != null && bytes.length >= 4 )
995
+ {
996
+
997
+ int head = ((int)bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
998
+ if( java.util.zip.GZIPInputStream.GZIP_MAGIC == head )
999
+ {
1000
+ java.io.ByteArrayInputStream bais = null;
1001
+ java.util.zip.GZIPInputStream gzis = null;
1002
+ java.io.ByteArrayOutputStream baos = null;
1003
+ byte[] buffer = new byte[2048];
1004
+ int length = 0;
1005
+
1006
+ try
1007
+ {
1008
+ baos = new java.io.ByteArrayOutputStream();
1009
+ bais = new java.io.ByteArrayInputStream( bytes );
1010
+ gzis = new java.util.zip.GZIPInputStream( bais );
1011
+
1012
+ while( ( length = gzis.read( buffer ) ) >= 0 )
1013
+ {
1014
+ baos.write(buffer,0,length);
1015
+ } // end while: reading input
1016
+
1017
+ // No error? Get new bytes.
1018
+ bytes = baos.toByteArray();
1019
+
1020
+ } // end try
1021
+ catch( java.io.IOException e )
1022
+ {
1023
+ // Just return originally-decoded bytes
1024
+ } // end catch
1025
+ finally
1026
+ {
1027
+ try{ baos.close(); } catch( Exception e ){}
1028
+ try{ gzis.close(); } catch( Exception e ){}
1029
+ try{ bais.close(); } catch( Exception e ){}
1030
+ } // end finally
1031
+
1032
+ } // end if: gzipped
1033
+ } // end if: bytes.length >= 2
1034
+
1035
+ return bytes;
1036
+ } // end decode
1037
+
1038
+
1039
+
1040
+
1041
+ /**
1042
+ * Attempts to decode Base64 data and deserialize a Java
1043
+ * Object within. Returns <tt>null</tt> if there was an error.
1044
+ *
1045
+ * @param encodedObject The Base64 data to decode
1046
+ * @return The decoded and deserialized object
1047
+ * @since 1.5
1048
+ */
1049
+ public static Object decodeToObject( String encodedObject )
1050
+ {
1051
+ // Decode and gunzip if necessary
1052
+ byte[] objBytes = decode( encodedObject );
1053
+
1054
+ java.io.ByteArrayInputStream bais = null;
1055
+ java.io.ObjectInputStream ois = null;
1056
+ Object obj = null;
1057
+
1058
+ try
1059
+ {
1060
+ bais = new java.io.ByteArrayInputStream( objBytes );
1061
+ ois = new java.io.ObjectInputStream( bais );
1062
+
1063
+ obj = ois.readObject();
1064
+ } // end try
1065
+ catch( java.io.IOException e )
1066
+ {
1067
+ e.printStackTrace();
1068
+ obj = null;
1069
+ } // end catch
1070
+ catch( java.lang.ClassNotFoundException e )
1071
+ {
1072
+ e.printStackTrace();
1073
+ obj = null;
1074
+ } // end catch
1075
+ finally
1076
+ {
1077
+ try{ bais.close(); } catch( Exception e ){}
1078
+ try{ ois.close(); } catch( Exception e ){}
1079
+ } // end finally
1080
+
1081
+ return obj;
1082
+ } // end decodeObject
1083
+
1084
+
1085
+
1086
+ /**
1087
+ * Convenience method for encoding data to a file.
1088
+ *
1089
+ * @param dataToEncode byte array of data to encode in base64 form
1090
+ * @param filename Filename for saving encoded data
1091
+ * @return <tt>true</tt> if successful, <tt>false</tt> otherwise
1092
+ *
1093
+ * @since 2.1
1094
+ */
1095
+ public static boolean encodeToFile( byte[] dataToEncode, String filename )
1096
+ {
1097
+ boolean success = false;
1098
+ Base64.OutputStream bos = null;
1099
+ try
1100
+ {
1101
+ bos = new Base64.OutputStream(
1102
+ new java.io.FileOutputStream( filename ), Base64.ENCODE );
1103
+ bos.write( dataToEncode );
1104
+ success = true;
1105
+ } // end try
1106
+ catch( java.io.IOException e )
1107
+ {
1108
+
1109
+ success = false;
1110
+ } // end catch: IOException
1111
+ finally
1112
+ {
1113
+ try{ bos.close(); } catch( Exception e ){}
1114
+ } // end finally
1115
+
1116
+ return success;
1117
+ } // end encodeToFile
1118
+
1119
+
1120
+ /**
1121
+ * Convenience method for decoding data to a file.
1122
+ *
1123
+ * @param dataToDecode Base64-encoded data as a string
1124
+ * @param filename Filename for saving decoded data
1125
+ * @return <tt>true</tt> if successful, <tt>false</tt> otherwise
1126
+ *
1127
+ * @since 2.1
1128
+ */
1129
+ public static boolean decodeToFile( String dataToDecode, String filename )
1130
+ {
1131
+ boolean success = false;
1132
+ Base64.OutputStream bos = null;
1133
+ try
1134
+ {
1135
+ bos = new Base64.OutputStream(
1136
+ new java.io.FileOutputStream( filename ), Base64.DECODE );
1137
+ bos.write( dataToDecode.getBytes( PREFERRED_ENCODING ) );
1138
+ success = true;
1139
+ } // end try
1140
+ catch( java.io.IOException e )
1141
+ {
1142
+ success = false;
1143
+ } // end catch: IOException
1144
+ finally
1145
+ {
1146
+ try{ bos.close(); } catch( Exception e ){}
1147
+ } // end finally
1148
+
1149
+ return success;
1150
+ } // end decodeToFile
1151
+
1152
+
1153
+
1154
+
1155
+ /**
1156
+ * Convenience method for reading a base64-encoded
1157
+ * file and decoding it.
1158
+ *
1159
+ * @param filename Filename for reading encoded data
1160
+ * @return decoded byte array or null if unsuccessful
1161
+ *
1162
+ * @since 2.1
1163
+ */
1164
+ public static byte[] decodeFromFile( String filename )
1165
+ {
1166
+ byte[] decodedData = null;
1167
+ Base64.InputStream bis = null;
1168
+ try
1169
+ {
1170
+ // Set up some useful variables
1171
+ java.io.File file = new java.io.File( filename );
1172
+ byte[] buffer = null;
1173
+ int length = 0;
1174
+ int numBytes = 0;
1175
+
1176
+ // Check for size of file
1177
+ if( file.length() > Integer.MAX_VALUE )
1178
+ {
1179
+ System.err.println( "File is too big for this convenience method (" + file.length() + " bytes)." );
1180
+ return null;
1181
+ } // end if: file too big for int index
1182
+ buffer = new byte[ (int)file.length() ];
1183
+
1184
+ // Open a stream
1185
+ bis = new Base64.InputStream(
1186
+ new java.io.BufferedInputStream(
1187
+ new java.io.FileInputStream( file ) ), Base64.DECODE );
1188
+
1189
+ // Read until done
1190
+ while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 )
1191
+ length += numBytes;
1192
+
1193
+ // Save in a variable to return
1194
+ decodedData = new byte[ length ];
1195
+ System.arraycopy( buffer, 0, decodedData, 0, length );
1196
+
1197
+ } // end try
1198
+ catch( java.io.IOException e )
1199
+ {
1200
+ System.err.println( "Error decoding from file " + filename );
1201
+ } // end catch: IOException
1202
+ finally
1203
+ {
1204
+ try{ bis.close(); } catch( Exception e) {}
1205
+ } // end finally
1206
+
1207
+ return decodedData;
1208
+ } // end decodeFromFile
1209
+
1210
+
1211
+
1212
+ /**
1213
+ * Convenience method for reading a binary file
1214
+ * and base64-encoding it.
1215
+ *
1216
+ * @param filename Filename for reading binary data
1217
+ * @return base64-encoded string or null if unsuccessful
1218
+ *
1219
+ * @since 2.1
1220
+ */
1221
+ public static String encodeFromFile( String filename )
1222
+ {
1223
+ String encodedData = null;
1224
+ Base64.InputStream bis = null;
1225
+ try
1226
+ {
1227
+ // Set up some useful variables
1228
+ java.io.File file = new java.io.File( filename );
1229
+ byte[] buffer = new byte[ Math.max((int)(file.length() * 1.4),40) ]; // Need max() for math on small files (v2.2.1)
1230
+ int length = 0;
1231
+ int numBytes = 0;
1232
+
1233
+ // Open a stream
1234
+ bis = new Base64.InputStream(
1235
+ new java.io.BufferedInputStream(
1236
+ new java.io.FileInputStream( file ) ), Base64.ENCODE );
1237
+
1238
+ // Read until done
1239
+ while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 )
1240
+ length += numBytes;
1241
+
1242
+ // Save in a variable to return
1243
+ encodedData = new String( buffer, 0, length, Base64.PREFERRED_ENCODING );
1244
+
1245
+ } // end try
1246
+ catch( java.io.IOException e )
1247
+ {
1248
+ System.err.println( "Error encoding from file " + filename );
1249
+ } // end catch: IOException
1250
+ finally
1251
+ {
1252
+ try{ bis.close(); } catch( Exception e) {}
1253
+ } // end finally
1254
+
1255
+ return encodedData;
1256
+ } // end encodeFromFile
1257
+
1258
+
1259
+
1260
+
1261
+ /**
1262
+ * Reads <tt>infile</tt> and encodes it to <tt>outfile</tt>.
1263
+ *
1264
+ * @param infile Input file
1265
+ * @param outfile Output file
1266
+ * @return true if the operation is successful
1267
+ * @since 2.2
1268
+ */
1269
+ public static boolean encodeFileToFile( String infile, String outfile )
1270
+ {
1271
+ boolean success = false;
1272
+ java.io.InputStream in = null;
1273
+ java.io.OutputStream out = null;
1274
+ try{
1275
+ in = new Base64.InputStream(
1276
+ new java.io.BufferedInputStream(
1277
+ new java.io.FileInputStream( infile ) ),
1278
+ Base64.ENCODE );
1279
+ out = new java.io.BufferedOutputStream( new java.io.FileOutputStream( outfile ) );
1280
+ byte[] buffer = new byte[65536]; // 64K
1281
+ int read = -1;
1282
+ while( ( read = in.read(buffer) ) >= 0 ){
1283
+ out.write( buffer,0,read );
1284
+ } // end while: through file
1285
+ success = true;
1286
+ } catch( java.io.IOException exc ){
1287
+ exc.printStackTrace();
1288
+ } finally{
1289
+ try{ in.close(); } catch( Exception exc ){}
1290
+ try{ out.close(); } catch( Exception exc ){}
1291
+ } // end finally
1292
+
1293
+ return success;
1294
+ } // end encodeFileToFile
1295
+
1296
+
1297
+
1298
+ /**
1299
+ * Reads <tt>infile</tt> and decodes it to <tt>outfile</tt>.
1300
+ *
1301
+ * @param infile Input file
1302
+ * @param outfile Output file
1303
+ * @return true if the operation is successful
1304
+ * @since 2.2
1305
+ */
1306
+ public static boolean decodeFileToFile( String infile, String outfile )
1307
+ {
1308
+ boolean success = false;
1309
+ java.io.InputStream in = null;
1310
+ java.io.OutputStream out = null;
1311
+ try{
1312
+ in = new Base64.InputStream(
1313
+ new java.io.BufferedInputStream(
1314
+ new java.io.FileInputStream( infile ) ),
1315
+ Base64.DECODE );
1316
+ out = new java.io.BufferedOutputStream( new java.io.FileOutputStream( outfile ) );
1317
+ byte[] buffer = new byte[65536]; // 64K
1318
+ int read = -1;
1319
+ while( ( read = in.read(buffer) ) >= 0 ){
1320
+ out.write( buffer,0,read );
1321
+ } // end while: through file
1322
+ success = true;
1323
+ } catch( java.io.IOException exc ){
1324
+ exc.printStackTrace();
1325
+ } finally{
1326
+ try{ in.close(); } catch( Exception exc ){}
1327
+ try{ out.close(); } catch( Exception exc ){}
1328
+ } // end finally
1329
+
1330
+ return success;
1331
+ } // end decodeFileToFile
1332
+
1333
+
1334
+ /* ******** I N N E R C L A S S I N P U T S T R E A M ******** */
1335
+
1336
+
1337
+
1338
+ /**
1339
+ * A {@link Base64.InputStream} will read data from another
1340
+ * <tt>java.io.InputStream</tt>, given in the constructor,
1341
+ * and encode/decode to/from Base64 notation on the fly.
1342
+ *
1343
+ * @see Base64
1344
+ * @since 1.3
1345
+ */
1346
+ public static class InputStream extends java.io.FilterInputStream
1347
+ {
1348
+ private boolean encode; // Encoding or decoding
1349
+ private int position; // Current position in the buffer
1350
+ private byte[] buffer; // Small buffer holding converted data
1351
+ private int bufferLength; // Length of buffer (3 or 4)
1352
+ private int numSigBytes; // Number of meaningful bytes in the buffer
1353
+ private int lineLength;
1354
+ private boolean breakLines; // Break lines at less than 80 characters
1355
+ private int options; // Record options used to create the stream.
1356
+ private byte[] alphabet; // Local copies to avoid extra method calls
1357
+ private byte[] decodabet; // Local copies to avoid extra method calls
1358
+
1359
+
1360
+ /**
1361
+ * Constructs a {@link Base64.InputStream} in DECODE mode.
1362
+ *
1363
+ * @param in the <tt>java.io.InputStream</tt> from which to read data.
1364
+ * @since 1.3
1365
+ */
1366
+ public InputStream( java.io.InputStream in )
1367
+ {
1368
+ this( in, DECODE );
1369
+ } // end constructor
1370
+
1371
+
1372
+ /**
1373
+ * Constructs a {@link Base64.InputStream} in
1374
+ * either ENCODE or DECODE mode.
1375
+ * <p>
1376
+ * Valid options:<pre>
1377
+ * ENCODE or DECODE: Encode or Decode as data is read.
1378
+ * DONT_BREAK_LINES: don't break lines at 76 characters
1379
+ * (only meaningful when encoding)
1380
+ * <i>Note: Technically, this makes your encoding non-compliant.</i>
1381
+ * </pre>
1382
+ * <p>
1383
+ * Example: <code>new Base64.InputStream( in, Base64.DECODE )</code>
1384
+ *
1385
+ *
1386
+ * @param in the <tt>java.io.InputStream</tt> from which to read data.
1387
+ * @param options Specified options
1388
+ * @see Base64#ENCODE
1389
+ * @see Base64#DECODE
1390
+ * @see Base64#DONT_BREAK_LINES
1391
+ * @since 2.0
1392
+ */
1393
+ public InputStream( java.io.InputStream in, int options )
1394
+ {
1395
+ super( in );
1396
+ this.breakLines = (options & DONT_BREAK_LINES) != DONT_BREAK_LINES;
1397
+ this.encode = (options & ENCODE) == ENCODE;
1398
+ this.bufferLength = encode ? 4 : 3;
1399
+ this.buffer = new byte[ bufferLength ];
1400
+ this.position = -1;
1401
+ this.lineLength = 0;
1402
+ this.options = options; // Record for later, mostly to determine which alphabet to use
1403
+ this.alphabet = getAlphabet(options);
1404
+ this.decodabet = getDecodabet(options);
1405
+ } // end constructor
1406
+
1407
+ /**
1408
+ * Reads enough of the input stream to convert
1409
+ * to/from Base64 and returns the next byte.
1410
+ *
1411
+ * @return next byte
1412
+ * @since 1.3
1413
+ */
1414
+ public int read() throws java.io.IOException
1415
+ {
1416
+ // Do we need to get data?
1417
+ if( position < 0 )
1418
+ {
1419
+ if( encode )
1420
+ {
1421
+ byte[] b3 = new byte[3];
1422
+ int numBinaryBytes = 0;
1423
+ for( int i = 0; i < 3; i++ )
1424
+ {
1425
+ try
1426
+ {
1427
+ int b = in.read();
1428
+
1429
+ // If end of stream, b is -1.
1430
+ if( b >= 0 )
1431
+ {
1432
+ b3[i] = (byte)b;
1433
+ numBinaryBytes++;
1434
+ } // end if: not end of stream
1435
+
1436
+ } // end try: read
1437
+ catch( java.io.IOException e )
1438
+ {
1439
+ // Only a problem if we got no data at all.
1440
+ if( i == 0 )
1441
+ throw e;
1442
+
1443
+ } // end catch
1444
+ } // end for: each needed input byte
1445
+
1446
+ if( numBinaryBytes > 0 )
1447
+ {
1448
+ encode3to4( b3, 0, numBinaryBytes, buffer, 0, options );
1449
+ position = 0;
1450
+ numSigBytes = 4;
1451
+ } // end if: got data
1452
+ else
1453
+ {
1454
+ return -1;
1455
+ } // end else
1456
+ } // end if: encoding
1457
+
1458
+ // Else decoding
1459
+ else
1460
+ {
1461
+ byte[] b4 = new byte[4];
1462
+ int i = 0;
1463
+ for( i = 0; i < 4; i++ )
1464
+ {
1465
+ // Read four "meaningful" bytes:
1466
+ int b = 0;
1467
+ do{ b = in.read(); }
1468
+ while( b >= 0 && decodabet[ b & 0x7f ] <= WHITE_SPACE_ENC );
1469
+
1470
+ if( b < 0 )
1471
+ break; // Reads a -1 if end of stream
1472
+
1473
+ b4[i] = (byte)b;
1474
+ } // end for: each needed input byte
1475
+
1476
+ if( i == 4 )
1477
+ {
1478
+ numSigBytes = decode4to3( b4, 0, buffer, 0, options );
1479
+ position = 0;
1480
+ } // end if: got four characters
1481
+ else if( i == 0 ){
1482
+ return -1;
1483
+ } // end else if: also padded correctly
1484
+ else
1485
+ {
1486
+ // Must have broken out from above.
1487
+ throw new java.io.IOException( "Improperly padded Base64 input." );
1488
+ } // end
1489
+
1490
+ } // end else: decode
1491
+ } // end else: get data
1492
+
1493
+ // Got data?
1494
+ if( position >= 0 )
1495
+ {
1496
+ // End of relevant data?
1497
+ if( /*!encode &&*/ position >= numSigBytes )
1498
+ return -1;
1499
+
1500
+ if( encode && breakLines && lineLength >= MAX_LINE_LENGTH )
1501
+ {
1502
+ lineLength = 0;
1503
+ return '\n';
1504
+ } // end if
1505
+ else
1506
+ {
1507
+ lineLength++; // This isn't important when decoding
1508
+ // but throwing an extra "if" seems
1509
+ // just as wasteful.
1510
+
1511
+ int b = buffer[ position++ ];
1512
+
1513
+ if( position >= bufferLength )
1514
+ position = -1;
1515
+
1516
+ return b & 0xFF; // This is how you "cast" a byte that's
1517
+ // intended to be unsigned.
1518
+ } // end else
1519
+ } // end if: position >= 0
1520
+
1521
+ // Else error
1522
+ else
1523
+ {
1524
+ // When JDK1.4 is more accepted, use an assertion here.
1525
+ throw new java.io.IOException( "Error in Base64 code reading stream." );
1526
+ } // end else
1527
+ } // end read
1528
+
1529
+
1530
+ /**
1531
+ * Calls {@link #read()} repeatedly until the end of stream
1532
+ * is reached or <var>len</var> bytes are read.
1533
+ * Returns number of bytes read into array or -1 if
1534
+ * end of stream is encountered.
1535
+ *
1536
+ * @param dest array to hold values
1537
+ * @param off offset for array
1538
+ * @param len max number of bytes to read into array
1539
+ * @return bytes read into array or -1 if end of stream is encountered.
1540
+ * @since 1.3
1541
+ */
1542
+ public int read( byte[] dest, int off, int len ) throws java.io.IOException
1543
+ {
1544
+ int i;
1545
+ int b;
1546
+ for( i = 0; i < len; i++ )
1547
+ {
1548
+ b = read();
1549
+
1550
+ //if( b < 0 && i == 0 )
1551
+ // return -1;
1552
+
1553
+ if( b >= 0 )
1554
+ dest[off + i] = (byte)b;
1555
+ else if( i == 0 )
1556
+ return -1;
1557
+ else
1558
+ break; // Out of 'for' loop
1559
+ } // end for: each byte read
1560
+ return i;
1561
+ } // end read
1562
+
1563
+ } // end inner class InputStream
1564
+
1565
+
1566
+
1567
+
1568
+
1569
+
1570
+ /* ******** I N N E R C L A S S O U T P U T S T R E A M ******** */
1571
+
1572
+
1573
+
1574
+ /**
1575
+ * A {@link Base64.OutputStream} will write data to another
1576
+ * <tt>java.io.OutputStream</tt>, given in the constructor,
1577
+ * and encode/decode to/from Base64 notation on the fly.
1578
+ *
1579
+ * @see Base64
1580
+ * @since 1.3
1581
+ */
1582
+ public static class OutputStream extends java.io.FilterOutputStream
1583
+ {
1584
+ private boolean encode;
1585
+ private int position;
1586
+ private byte[] buffer;
1587
+ private int bufferLength;
1588
+ private int lineLength;
1589
+ private boolean breakLines;
1590
+ private byte[] b4; // Scratch used in a few places
1591
+ private boolean suspendEncoding;
1592
+ private int options; // Record for later
1593
+ private byte[] alphabet; // Local copies to avoid extra method calls
1594
+ private byte[] decodabet; // Local copies to avoid extra method calls
1595
+
1596
+ /**
1597
+ * Constructs a {@link Base64.OutputStream} in ENCODE mode.
1598
+ *
1599
+ * @param out the <tt>java.io.OutputStream</tt> to which data will be written.
1600
+ * @since 1.3
1601
+ */
1602
+ public OutputStream( java.io.OutputStream out )
1603
+ {
1604
+ this( out, ENCODE );
1605
+ } // end constructor
1606
+
1607
+
1608
+ /**
1609
+ * Constructs a {@link Base64.OutputStream} in
1610
+ * either ENCODE or DECODE mode.
1611
+ * <p>
1612
+ * Valid options:<pre>
1613
+ * ENCODE or DECODE: Encode or Decode as data is read.
1614
+ * DONT_BREAK_LINES: don't break lines at 76 characters
1615
+ * (only meaningful when encoding)
1616
+ * <i>Note: Technically, this makes your encoding non-compliant.</i>
1617
+ * </pre>
1618
+ * <p>
1619
+ * Example: <code>new Base64.OutputStream( out, Base64.ENCODE )</code>
1620
+ *
1621
+ * @param out the <tt>java.io.OutputStream</tt> to which data will be written.
1622
+ * @param options Specified options.
1623
+ * @see Base64#ENCODE
1624
+ * @see Base64#DECODE
1625
+ * @see Base64#DONT_BREAK_LINES
1626
+ * @since 1.3
1627
+ */
1628
+ public OutputStream( java.io.OutputStream out, int options )
1629
+ {
1630
+ super( out );
1631
+ this.breakLines = (options & DONT_BREAK_LINES) != DONT_BREAK_LINES;
1632
+ this.encode = (options & ENCODE) == ENCODE;
1633
+ this.bufferLength = encode ? 3 : 4;
1634
+ this.buffer = new byte[ bufferLength ];
1635
+ this.position = 0;
1636
+ this.lineLength = 0;
1637
+ this.suspendEncoding = false;
1638
+ this.b4 = new byte[4];
1639
+ this.options = options;
1640
+ this.alphabet = getAlphabet(options);
1641
+ this.decodabet = getDecodabet(options);
1642
+ } // end constructor
1643
+
1644
+
1645
+ /**
1646
+ * Writes the byte to the output stream after
1647
+ * converting to/from Base64 notation.
1648
+ * When encoding, bytes are buffered three
1649
+ * at a time before the output stream actually
1650
+ * gets a write() call.
1651
+ * When decoding, bytes are buffered four
1652
+ * at a time.
1653
+ *
1654
+ * @param theByte the byte to write
1655
+ * @since 1.3
1656
+ */
1657
+ public void write(int theByte) throws java.io.IOException
1658
+ {
1659
+ // Encoding suspended?
1660
+ if( suspendEncoding )
1661
+ {
1662
+ super.out.write( theByte );
1663
+ return;
1664
+ } // end if: supsended
1665
+
1666
+ // Encode?
1667
+ if( encode )
1668
+ {
1669
+ buffer[ position++ ] = (byte)theByte;
1670
+ if( position >= bufferLength ) // Enough to encode.
1671
+ {
1672
+ out.write( encode3to4( b4, buffer, bufferLength, options ) );
1673
+
1674
+ lineLength += 4;
1675
+ if( breakLines && lineLength >= MAX_LINE_LENGTH )
1676
+ {
1677
+ out.write( NEW_LINE );
1678
+ lineLength = 0;
1679
+ } // end if: end of line
1680
+
1681
+ position = 0;
1682
+ } // end if: enough to output
1683
+ } // end if: encoding
1684
+
1685
+ // Else, Decoding
1686
+ else
1687
+ {
1688
+ // Meaningful Base64 character?
1689
+ if( decodabet[ theByte & 0x7f ] > WHITE_SPACE_ENC )
1690
+ {
1691
+ buffer[ position++ ] = (byte)theByte;
1692
+ if( position >= bufferLength ) // Enough to output.
1693
+ {
1694
+ int len = Base64.decode4to3( buffer, 0, b4, 0, options );
1695
+ out.write( b4, 0, len );
1696
+ //out.write( Base64.decode4to3( buffer ) );
1697
+ position = 0;
1698
+ } // end if: enough to output
1699
+ } // end if: meaningful base64 character
1700
+ else if( decodabet[ theByte & 0x7f ] != WHITE_SPACE_ENC )
1701
+ {
1702
+ throw new java.io.IOException( "Invalid character in Base64 data." );
1703
+ } // end else: not white space either
1704
+ } // end else: decoding
1705
+ } // end write
1706
+
1707
+
1708
+
1709
+ /**
1710
+ * Calls {@link #write(int)} repeatedly until <var>len</var>
1711
+ * bytes are written.
1712
+ *
1713
+ * @param theBytes array from which to read bytes
1714
+ * @param off offset for array
1715
+ * @param len max number of bytes to read into array
1716
+ * @since 1.3
1717
+ */
1718
+ public void write( byte[] theBytes, int off, int len ) throws java.io.IOException
1719
+ {
1720
+ // Encoding suspended?
1721
+ if( suspendEncoding )
1722
+ {
1723
+ super.out.write( theBytes, off, len );
1724
+ return;
1725
+ } // end if: supsended
1726
+
1727
+ for( int i = 0; i < len; i++ )
1728
+ {
1729
+ write( theBytes[ off + i ] );
1730
+ } // end for: each byte written
1731
+
1732
+ } // end write
1733
+
1734
+
1735
+
1736
+ /**
1737
+ * Method added by PHIL. [Thanks, PHIL. -Rob]
1738
+ * This pads the buffer without closing the stream.
1739
+ */
1740
+ public void flushBase64() throws java.io.IOException
1741
+ {
1742
+ if( position > 0 )
1743
+ {
1744
+ if( encode )
1745
+ {
1746
+ out.write( encode3to4( b4, buffer, position, options ) );
1747
+ position = 0;
1748
+ } // end if: encoding
1749
+ else
1750
+ {
1751
+ throw new java.io.IOException( "Base64 input not properly padded." );
1752
+ } // end else: decoding
1753
+ } // end if: buffer partially full
1754
+
1755
+ } // end flush
1756
+
1757
+
1758
+ /**
1759
+ * Flushes and closes (I think, in the superclass) the stream.
1760
+ *
1761
+ * @since 1.3
1762
+ */
1763
+ public void close() throws java.io.IOException
1764
+ {
1765
+ // 1. Ensure that pending characters are written
1766
+ flushBase64();
1767
+
1768
+ // 2. Actually close the stream
1769
+ // Base class both flushes and closes.
1770
+ super.close();
1771
+
1772
+ buffer = null;
1773
+ out = null;
1774
+ } // end close
1775
+
1776
+
1777
+
1778
+ /**
1779
+ * Suspends encoding of the stream.
1780
+ * May be helpful if you need to embed a piece of
1781
+ * base640-encoded data in a stream.
1782
+ *
1783
+ * @since 1.5.1
1784
+ */
1785
+ public void suspendEncoding() throws java.io.IOException
1786
+ {
1787
+ flushBase64();
1788
+ this.suspendEncoding = true;
1789
+ } // end suspendEncoding
1790
+
1791
+
1792
+ /**
1793
+ * Resumes encoding of the stream.
1794
+ * May be helpful if you need to embed a piece of
1795
+ * base640-encoded data in a stream.
1796
+ *
1797
+ * @since 1.5.1
1798
+ */
1799
+ public void resumeEncoding()
1800
+ {
1801
+ this.suspendEncoding = false;
1802
+ } // end resumeEncoding
1803
+
1804
+
1805
+
1806
+ } // end inner class OutputStream
1807
+
1808
+
1809
+ } // end class Base64