PageRenderTime 149ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/openstego-src-0.5.2/src/net/sourceforge/openstego/util/CommonUtil.java

#
Java | 385 lines | 301 code | 26 blank | 58 comment | 18 complexity | c0af326226cebcc0f537deeb8ba8e94b MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * Steganography utility to hide messages into cover files
  3. * Author: Samir Vaidya (mailto:syvaidya@gmail.com)
  4. * Copyright (c) 2007-2008 Samir Vaidya
  5. */
  6. package net.sourceforge.openstego.util;
  7. import java.awt.Color;
  8. import java.io.ByteArrayOutputStream;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.FileOutputStream;
  12. import java.io.FilenameFilter;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.io.OutputStream;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import java.util.StringTokenizer;
  19. import javax.swing.JTextField;
  20. import javax.swing.UIManager;
  21. import net.sourceforge.openstego.OpenStegoException;
  22. /**
  23. * Common utilities for OpenStego
  24. */
  25. public class CommonUtil
  26. {
  27. /**
  28. * Constructor is private so that this class is not instantiated
  29. */
  30. private CommonUtil()
  31. {
  32. }
  33. /**
  34. * Method to get byte array data from given InputStream
  35. * @param is InputStream to read
  36. * @return Stream data as byte array
  37. * @throws OpenStegoException
  38. */
  39. public static byte[] getStreamBytes(InputStream is) throws OpenStegoException
  40. {
  41. final int BUF_SIZE = 512;
  42. ByteArrayOutputStream bos = null;
  43. int bytesRead = 0;
  44. byte[] data = null;
  45. try
  46. {
  47. data = new byte[BUF_SIZE];
  48. bos = new ByteArrayOutputStream();
  49. while((bytesRead = is.read(data, 0, BUF_SIZE)) >= 0)
  50. {
  51. bos.write(data, 0, bytesRead);
  52. }
  53. is.close();
  54. bos.close();
  55. return bos.toByteArray();
  56. }
  57. catch(IOException ioEx)
  58. {
  59. throw new OpenStegoException(ioEx);
  60. }
  61. }
  62. /**
  63. * Method to get byte array data from given file
  64. * @param file File to read
  65. * @return File data as byte array
  66. * @throws OpenStegoException
  67. */
  68. public static byte[] getFileBytes(File file) throws OpenStegoException
  69. {
  70. try
  71. {
  72. return getStreamBytes(new FileInputStream(file));
  73. }
  74. catch(IOException ioEx)
  75. {
  76. throw new OpenStegoException(ioEx);
  77. }
  78. }
  79. /**
  80. * Method to write file data to disk
  81. * @param fileData File data
  82. * @param fileName File name (If this is <code>null</code>, then data is written to stdout)
  83. * @throws OpenStegoException
  84. */
  85. public static void writeFile(byte[] fileData, String fileName) throws OpenStegoException
  86. {
  87. File file = null;
  88. if(fileName != null)
  89. {
  90. file = new File(fileName);
  91. }
  92. writeFile(fileData, file);
  93. }
  94. /**
  95. * Method to write file data to disk
  96. * @param fileData File data
  97. * @param file File object (If this is <code>null</code>, then data is written to stdout)
  98. * @throws OpenStegoException
  99. */
  100. public static void writeFile(byte[] fileData, File file) throws OpenStegoException
  101. {
  102. OutputStream os = null;
  103. try
  104. {
  105. // If file is not provided, then write the data to stdout
  106. if(file == null)
  107. {
  108. os = System.out;
  109. }
  110. else
  111. {
  112. os = new FileOutputStream(file);
  113. }
  114. os.write(fileData);
  115. os.close();
  116. }
  117. catch(IOException ioEx)
  118. {
  119. throw new OpenStegoException(ioEx);
  120. }
  121. }
  122. /**
  123. * Method to enable/disable a Swing JTextField object
  124. * @param textField Swing JTextField object
  125. * @param enabled Flag to indicate whether to enable or disable the object
  126. */
  127. public static void setEnabled(JTextField textField, boolean enabled)
  128. {
  129. if(enabled)
  130. {
  131. textField.setEnabled(true);
  132. textField.setBackground(Color.WHITE);
  133. }
  134. else
  135. {
  136. textField.setEnabled(false);
  137. textField.setBackground(UIManager.getColor("Panel.background"));
  138. }
  139. }
  140. /**
  141. * Method to parse a delimiter separated list of files into arraylist of filenames. It supports wildcard characters
  142. * "*" and "?" within the filenames.
  143. * @param fileList Delimiter separated list of filenames
  144. * @param delimiter Delimiter for tokenization
  145. * @return List of filenames after tokenizing and wildcard expansion
  146. */
  147. public static List parseFileList(String fileList, String delimiter)
  148. {
  149. int index = 0;
  150. StringTokenizer tokenizer = null;
  151. String fileName = null;
  152. String dirName = null;
  153. ArrayList output = new ArrayList();
  154. File fileDir = null;
  155. File[] arrFile = null;
  156. if(fileList == null)
  157. {
  158. return output;
  159. }
  160. tokenizer = new StringTokenizer(fileList, delimiter);
  161. while(tokenizer.hasMoreTokens())
  162. {
  163. fileName = tokenizer.nextToken().trim();
  164. index = fileName.lastIndexOf(File.separator);
  165. if(index >= 0)
  166. {
  167. dirName = fileName.substring(0, index);
  168. fileName = fileName.substring(index + 1);
  169. }
  170. else
  171. {
  172. dirName = ".";
  173. }
  174. fileName = replaceWildcards(fileName);
  175. fileDir = new File(dirName.equals("") ? "." : dirName);
  176. arrFile = fileDir.listFiles(new WildcardFilenameFilter(fileName));
  177. for(int i = 0; i < arrFile.length; i++)
  178. {
  179. output.add(arrFile[i]);
  180. }
  181. }
  182. return output;
  183. }
  184. /**
  185. * Byte to Int converter
  186. * @param b Input byte value
  187. * @return Int value
  188. */
  189. public static int byteToInt(int b)
  190. {
  191. int i = b;
  192. if(i < 0)
  193. {
  194. i = i + 256;
  195. }
  196. return i;
  197. }
  198. /**
  199. * Helper method to replace file wildcard characters with Java regexp wildcard chararcters
  200. * @param input Input String
  201. * @return String containing modified wildcard characters
  202. */
  203. private static String replaceWildcards(String input)
  204. {
  205. StringBuffer buffer = new StringBuffer();
  206. char[] chars = input.toCharArray();
  207. for(int i = 0; i < chars.length; i++)
  208. {
  209. if(chars[i] == '*')
  210. {
  211. buffer.append(".*");
  212. }
  213. else if(chars[i] == '?')
  214. {
  215. buffer.append(".{1}");
  216. }
  217. else if("+()^$.{}[]|\\".indexOf(chars[i]) != -1) // Escape rest of the java regexp wildcards
  218. {
  219. buffer.append('\\').append(chars[i]);
  220. }
  221. else
  222. {
  223. buffer.append(chars[i]);
  224. }
  225. }
  226. return buffer.toString();
  227. }
  228. /**
  229. * Inner class for wildcard filename filter
  230. */
  231. static class WildcardFilenameFilter implements FilenameFilter
  232. {
  233. /**
  234. * Variable to hold the filter string
  235. */
  236. String filter = null;
  237. /**
  238. * Default constructor
  239. * @param filter Filter string
  240. */
  241. public WildcardFilenameFilter(String filter)
  242. {
  243. this.filter = filter.toLowerCase();
  244. }
  245. /**
  246. * Implementation of <code>accept</code> method
  247. * @param dir Directory to traverse
  248. * @param name Name of the file
  249. * @return Whether file is accepted by the filter or not
  250. */
  251. public boolean accept(File dir, String name)
  252. {
  253. return (name.toLowerCase().matches(filter));
  254. }
  255. }
  256. /**
  257. * Returns the floor of the half of the input value
  258. *
  259. * @param num Input number
  260. * @return Floor of the half of the input number
  261. */
  262. public static int floorHalf(int num)
  263. {
  264. if((num & 1) == 1)
  265. {
  266. return (num - 1) / 2;
  267. }
  268. else
  269. {
  270. return num / 2;
  271. }
  272. }
  273. /**
  274. * Returns the ceiling of the half of the input value
  275. *
  276. * @param num Input number
  277. * @return Ceiling of the half of the input number
  278. */
  279. public static int ceilingHalf(int num)
  280. {
  281. if((num & 1) == 1)
  282. {
  283. return (num + 1) / 2;
  284. }
  285. else
  286. {
  287. return num / 2;
  288. }
  289. }
  290. /**
  291. * Returns the modulus of the input value (taking care of the sign of the value)
  292. *
  293. * @param num Input number
  294. * @param div Divisor for modulus
  295. * @return Modulus of num by div
  296. */
  297. public static int mod(int num, int div)
  298. {
  299. if(num < 0)
  300. {
  301. return div - (-num % div);
  302. }
  303. else
  304. {
  305. return num % div;
  306. }
  307. }
  308. /**
  309. * Get maximum of two given values
  310. * @param x Value 1
  311. * @param y value 2
  312. * @return Max of the two values
  313. */
  314. public static int max(int x, int y)
  315. {
  316. return (x > y) ? x : y;
  317. }
  318. /**
  319. * Get maximum of two given values
  320. * @param x Value 1
  321. * @param y value 2
  322. * @return Max of the two values
  323. */
  324. public static double max(double x, double y)
  325. {
  326. return (x > y) ? x : y;
  327. }
  328. /**
  329. * Get minimum of two given values
  330. * @param x Value 1
  331. * @param y value 2
  332. * @return Min of the two values
  333. */
  334. public static int min(int x, int y)
  335. {
  336. return (x < y) ? x : y;
  337. }
  338. /**
  339. * Get minimum of two given values
  340. * @param x Value 1
  341. * @param y value 2
  342. * @return Min of the two values
  343. */
  344. public static double min(double x, double y)
  345. {
  346. return (x < y) ? x : y;
  347. }
  348. }