PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/installer/InstallThread.java

#
Java | 136 lines | 88 code | 25 blank | 23 comment | 9 complexity | 7333f09a37fdcd7347a27aed2545b5d9 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * InstallThread.java
  3. * Copyright (C) 1999, 2000 Slava Pestov
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package installer;
  20. import java.io.*;
  21. import java.util.Vector;
  22. /*
  23. * The thread that performs installation.
  24. */
  25. public class InstallThread extends Thread
  26. {
  27. public InstallThread(Install installer, Progress progress,
  28. String installDir, String binDir, int size, Vector components)
  29. {
  30. super("Install thread");
  31. this.installer = installer;
  32. this.progress = progress;
  33. this.installDir = installDir;
  34. this.binDir = binDir;
  35. this.size = size;
  36. this.components = components;
  37. buf = new byte[32768];
  38. }
  39. public void setProgress(Progress progress)
  40. {
  41. this.progress = progress;
  42. }
  43. public void run()
  44. {
  45. progress.setMaximum(size * 1024);
  46. try
  47. {
  48. for(int i = 0; i < components.size(); i++)
  49. {
  50. installComponent((String)components.elementAt(i));
  51. }
  52. // create it in case it doesn't already exist
  53. if(binDir != null)
  54. OperatingSystem.getOperatingSystem().mkdirs(binDir);
  55. OperatingSystem.getOperatingSystem().createScript(
  56. installer,installDir,binDir,
  57. installer.getProperty("app.name"));
  58. }
  59. catch(IOException io)
  60. {
  61. progress.error(io.toString());
  62. return;
  63. }
  64. progress.done();
  65. }
  66. // private members
  67. private Install installer;
  68. private Progress progress;
  69. private String installDir;
  70. private String binDir;
  71. private int size;
  72. private Vector components;
  73. private byte[] buf;
  74. private void installComponent(String name) throws IOException
  75. {
  76. BufferedReader fileList = new BufferedReader(
  77. new InputStreamReader(getClass()
  78. .getResourceAsStream(name)));
  79. String fileName;
  80. while((fileName = fileList.readLine()) != null)
  81. {
  82. String outfile = installDir + File.separatorChar
  83. + fileName.replace('/',File.separatorChar);
  84. InputStream in = new BufferedInputStream(
  85. getClass().getResourceAsStream("/" + fileName));
  86. if(in == null)
  87. throw new FileNotFoundException(fileName);
  88. copy(in,outfile);
  89. in.close();
  90. }
  91. fileList.close();
  92. }
  93. private void copy(InputStream in, String outfile) throws IOException
  94. {
  95. File outFile = new File(outfile);
  96. OperatingSystem.getOperatingSystem().mkdirs(outFile.getParent());
  97. BufferedOutputStream out = new BufferedOutputStream(
  98. new FileOutputStream(outFile));
  99. int count;
  100. for(;;)
  101. {
  102. count = in.read(buf,0,buf.length);
  103. if(count == -1)
  104. break;
  105. out.write(buf,0,count);
  106. progress.advance(count);
  107. }
  108. in.close();
  109. out.close();
  110. }
  111. }