/** * Get binary data of a source file. * * @param path The canonical path of source file. * @return Source file data. */ public byte[] read(String path) { if (!binaryCache.containsKey(path)) { try { BufferedInputStream bf = new BufferedInputStream(new FileInputStream(new File(path))); try { byte[] data = new byte[bf.available()]; bf.read(data); detectBOM(data, path); binaryCache.put(path, data); } finally { bf.close(); } } catch (IOException e) { App.exit(e); } } return binaryCache.get(path); }
public static void main(String[] args) throws IOException { int servPort = Integer.parseInt(args[0]); String ksName = "keystore.jks"; char ksPass[] = "password".toCharArray(); char ctPass[] = "password".toCharArray(); try { KeyStore ks = KeyStore.getInstance("JKS"); ks.load(new FileInputStream(ksName), ksPass); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); kmf.init(ks, ctPass); SSLContext sc = SSLContext.getInstance("SSL"); sc.init(kmf.getKeyManagers(), null, null); SSLServerSocketFactory ssf = sc.getServerSocketFactory(); SSLServerSocket s = (SSLServerSocket) ssf.createServerSocket(servPort); while (true) { // Listen for a TCP connection request. SSLSocket c = (SSLSocket) s.accept(); BufferedOutputStream out = new BufferedOutputStream(c.getOutputStream(), 1024); BufferedInputStream in = new BufferedInputStream(c.getInputStream(), 1024); byte[] byteBuffer = new byte[1024]; int count = 0; while ((byteBuffer[count] = (byte) in.read()) != -2) { count++; } String newFile = new String(byteBuffer, 0, count, "US-ASCII"); FileOutputStream writer = new FileOutputStream(newFile.trim()); int buffSize = 0; while ((buffSize = in.read(byteBuffer, 0, 1024)) != -1) { int index = 0; if ((index = (new String(byteBuffer, 0, buffSize, "US-ASCII")) .indexOf("------MagicStringCSE283Miami")) == -1) { writer.write(byteBuffer, 0, buffSize); } else { writer.write(byteBuffer, 0, index); break; } } writer.flush(); writer.close(); ZipOutputStream outZip = new ZipOutputStream(new BufferedOutputStream(out)); FileInputStream fin = new FileInputStream(newFile.trim()); BufferedInputStream origin = new BufferedInputStream(fin, 1024); ZipEntry entry = new ZipEntry(newFile.trim()); outZip.putNextEntry(entry); byteBuffer = new byte[1024]; int bytes = 0; while ((bytes = origin.read(byteBuffer, 0, 1024)) != -1) { outZip.write(byteBuffer, 0, bytes); } origin.close(); outZip.flush(); outZip.close(); out.flush(); out.close(); } } catch (Exception e) { System.err.println(e.toString()); } }