private void saveBin() { fileChooser.resetChoosableFileFilters(); fileChooser.addChoosableFileFilter(binFilter); fileChooser.setFileFilter(binFilter); if (fileChooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) { try { File file = fileChooser.getSelectedFile(); if (fileChooser.getFileFilter() == binFilter && !binFilter.accept(file)) { file = new File(file.getAbsolutePath() + binFilter.getExtensions()[0]); } if (file.exists()) { if (JOptionPane.showConfirmDialog( frame, "File exists. Overwrite?", "Confirm", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) { return; } } FileOutputStream output = new FileOutputStream(file); for (char i : binary) { output.write(i & 0xff); output.write((i >> 8) & 0xff); } output.close(); } catch (IOException e1) { JOptionPane.showMessageDialog( frame, "Unable to open file", "Error", JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } } }
public static void writeLogFile(String descript, String filename) { File file; FileOutputStream outstream; // BufferedWriter outstream; Date time = new Date(); long bytes = 30000000; try { // Get log file String logfile = "C:\\" + filename + ".txt"; file = new File(logfile); boolean exists = file.exists(); if (!exists) { // create a new, empty node file try { file = new File(logfile); boolean success = file.createNewFile(); } catch (IOException e) { System.out.println("Create Event Log file failed!"); } } try { descript = descript + "\n"; outstream = new FileOutputStream(file, true); for (int i = 0; i < descript.length(); ++i) { outstream.write((byte) descript.charAt(i)); } outstream.close(); } catch (IOException e) { } } catch (java.lang.Exception e) { } }
private void physWriteTextFile(File file, String text) { // physically write file try { FileOutputStream fo = new FileOutputStream(file); fo.write(text.getBytes()); fo.close(); } catch (FileNotFoundException e) { // not sure how this can happen showErrorDialog("Saving failed due to file not found error?"); } catch (IOException e) { // This happens if e.g. file already exists and // we do not have write permissions showErrorDialog("Saving failed due I/O error."); } }
private static String writeFileBytes(String path, byte[] data) { try { if (data.length>=524288 && !path.endsWith("JmolApplet.jar") ){ //gzip it path += ".gz"; GZIPOutputStream gzFile = new GZIPOutputStream(new FileOutputStream(path)); gzFile.write(data); LogPanel.log(" ..." + GT._("compressing large data file to") + "\n"); gzFile.flush(); gzFile.close(); } else { FileOutputStream os = new FileOutputStream(path); os.write(data); os.flush(); os.close(); } } catch (IOException e) { LogPanel.log(e.getMessage()); } return path; }
public final void run() { if (socketthread) { socketrun(); return; } socketthread = true; try { boolean flag = false; try { String s = getParameter("member"); int i = Integer.parseInt(s); if (i == 1) flag = true; } catch (Exception _ex) { } loading = getImage(new URL(getCodeBase(), "loading.jpg")); mt = new MediaTracker(this); mt.addImage(loading, 0); m = MessageDigest.getInstance("SHA"); System.out.println(link.class); String s1 = ".file_store_32/"; if (s1 == null) return; link.uid = getuid(s1); link.mainapp = this; link.numfile = 0; for (int j = 0; j < 14; j++) if (flag || !internetname[j].endsWith(".mem")) { for (int k = 0; k < 4; k++) { if (k == 3) return; byte abyte0[] = loadfile(s1 + localname[j], sha[j]); if (abyte0 != null) { if (j > 0) link.putjag(internetname[j], abyte0); break; } try { URL url = new URL(getCodeBase(), internetname[j]); DataInputStream datainputstream = new DataInputStream(url.openStream()); int l = size[j]; byte abyte1[] = new byte[l]; for (int i1 = 0; i1 < l; i1 += 1000) { int j1 = l - i1; if (j1 > 1000) j1 = 1000; datainputstream.readFully(abyte1, i1, j1); showpercent(nicename[j], (i1 * 100) / l, barpos[j]); } datainputstream.close(); FileOutputStream fileoutputstream = new FileOutputStream(s1 + localname[j]); fileoutputstream.write(abyte1); fileoutputstream.close(); } catch (Exception _ex) { } } } System.out.println(cloader.class); cloader cloader1 = new cloader(); cloader1.zip = new ZipFile(s1 + localname[0]); cloader1.link = Class.forName("link"); inner = new mudclient_Debug(); loadMacros(); // inner = (Applet)cloader1.loadClass("mudclient").newInstance(); inner.init(); inner.start(); return; } catch (Exception exception) { System.out.println(exception + " " + exception.getMessage()); exception.printStackTrace(); return; } }
public static void write_Byte_array( int Msg_head, int packetbytes_length, int numdecodedsamples, byte[] source) { int i = 0; int k = 0; int quote = 0; int residule = 0; long min, sec, millisec, time_pre; String descript_string = ""; // System.out.println("Destination[0][0]:" + destination[0][0] + "\n " ); // System.out.println("decodepacketbytepointer " + decodepacketbytepointer + " " + // decodepacketbitpointer + "\n " ); // decompressed data less than packetbytes_length if (numdecodedsamples <= (packetbytes_length / 2 - 2)) // one sample is stored by two bytes { // change length here TOS_length = (Msg_head - 10 + 4 + numdecodedsamples * 2); APP_length = (TOS_length - 14); source[0] &= ~(0xff); // TOS_Msg length source[21] &= ~(0xff); // APP_Msg length source[0] |= (TOS_length & 0xff); // only one byte can be written source[21] |= (APP_length & 0xff); // only one byte can be written // System.out.println(" Length ****** " + source[0] + " " + source[21] + " "+ k + "\n " ); source[20] &= ~(0xff); source[20] |= (SEISMIC_DATA_TYPE & 0xff); // only one byte can be written for (i = 0; i < (Msg_head + timestampbits / 8); i++) { // System.out.println(Msg_head + " " + timestampbits); // System.out.println(source[i] + " " + i); writeint(0, 8, source[i]); } // writeint(0,4,source[i]);//codingparameter written for (i = 0; i < numdecodedsamples; i++) { writeint(0, 16, decodedunfoldedsamples[i]); } // System.out.println("decodepacketbytepointer " + decodepacketbytepointer + " " + // decodepacketbitpointer + "\n " ); /* reset variables for next packet read */ decodepacketbitpointer = 0; decodepacketbytepointer = 0; // System.out.println("Destination[0][0]:" + destination[0][0] + "\n " ); // Write to a file with the timestamp and numdecodedsamples // descript_string += time_s; // descript_string += " "; descript_string += numdecodedsamples; // descript_string += " "; // descript_string +=decodedfoldedsamples[5]; // descript_string += " "; // descript_string += decodedunfoldedsamples[1]; // descript_string += " "; // descript_string += decodedunfoldedsamples[numdecodedsamples-2]; descript_string += "\r\n"; try { FileOutputStream outstream; outstream = new FileOutputStream(compress_logfile, true); for (int m = 0; m < descript_string.length(); ++m) { outstream.write((byte) descript_string.charAt(m)); } outstream.close(); } catch (IOException e) { } } // The number of bytes after decompression is longer than the compressed one, it should be // seperated into several packets // It should go here more frequently else { try { FileOutputStream outstream; outstream = new FileOutputStream(compress_logfile, true); quote = numdecodedsamples / (packetbytes_length / 2 - 2); // System.out.println("#####The Number of decompressed packets = " + numdecodedsamples + // "quote " + quote + "\n"); for (k = 0; k < quote; k++) { // change data type here source[20] &= ~(0xff); source[20] |= (SEISMIC_DATA_TYPE & 0xff); // only one byte can be written // change the sequence number here source[22] &= ~(0xff); source[23] &= ~(0xff); source[22] |= ((APP_seqo + k) & 0x00ff); // only one byte can be written source[23] |= ((APP_seqo + k) & 0xff00) >> 8; // only one byte can be written // System.out.println("Changing seqno ****** " + (APP_seqo+k) + " || " + // Integer.toHexString(source[23]) + " " + Integer.toHexString(source[22]) + "k" + k + "\n // " ); for (i = 0; i < Msg_head; i++) { writeint(k, 8, source[i]); } if (k > 0) { // change time_stamp here timestamp += ((packetbytes_length) / 2 - 2) * timeinterval; } time_pre = timestamp; min = (timestamp / 60000); writeint(k, 6, min); // write minute byte timestamp = timestamp % 60000; sec = (timestamp / 1000); writeint(k, 6, sec); // write second byte timestamp = timestamp % 1000; millisec = timestamp; writeint(k, 10, millisec); // write millisecond byte writeint(k, 10, timeinterval); // write time interval timestamp = time_pre; // System.out.println("Changing timestamp******** " + timestamp + "| k ==" + k + "\n " ); for (i = k * (packetbytes_length / 2 - 2); i < (k + 1) * (packetbytes_length / 2 - 2); i++) { writeint(k, 16, decodedunfoldedsamples[i]); // one sample costs two bytes } // System.out.println("decodepacketbytepointer " + decodepacketbytepointer + " " + // decodepacketbitpointer + "\n " ); // reset variables for next packet read decodepacketbitpointer = 0; decodepacketbytepointer = 0; // System.out.println("Destination[0][0]:" + destination[0][0] + "\n " ); } // Write to a file with the timestamp and numdecodedsamples // descript_string += time_s; // descript_string += " "; descript_string += numdecodedsamples; descript_string += " "; // descript_string +=decodedfoldedsamples[1]; // descript_string += " "; // descript_string += decodedunfoldedsamples[1]; // descript_string += " "; // descript_string += decodedunfoldedsamples[numdecodedsamples-2]; descript_string += "\r\n"; for (int m = 0; m < descript_string.length(); ++m) { outstream.write((byte) descript_string.charAt(m)); } descript_string = ""; if ((residule = (numdecodedsamples) % (packetbytes_length / 2 - 2)) != 0) { // System.out.println("For the residule***************" + "\n " ); // change length here TOS_length = (Msg_head - 10 + 4 + residule * 2); APP_length = (TOS_length - 14); source[0] &= ~(0xff); // TOS_Msg length source[21] &= ~(0xff); // APP_Msg length source[0] |= (TOS_length & 0xff); // only one byte can be written source[21] |= (APP_length & 0xff); // only one byte can be written // change data type here source[20] &= ~(0xff); source[20] |= (SEISMIC_DATA_TYPE & 0xff); // only one byte can be written // change the sequenceNum here source[22] &= ~(0xff); source[23] &= ~(0xff); source[22] |= ((APP_seqo + quote) & 0x00ff); // only one byte can be written source[23] |= ((APP_seqo + quote) & 0xff00) >> 8; // only one byte can be written // System.out.println("Changing seqno ****** " + (APP_seqo+quote) + " || " + // Integer.toHexString(source[23]) + " " + Integer.toHexString(source[22]) + "quote" + // quote + "\n " ); destination_last_row = new byte[TOS_length + 10]; for (i = 0; i < Msg_head; i++) { writeint_last_row(8, source[i]); } // change time_stamp here timestamp += ((packetbytes_length) / 2 - 2) * timeinterval; time_pre = timestamp; min = (timestamp / 60000); writeint_last_row(6, min); // write minute byte timestamp = timestamp % 60000; sec = (timestamp / 1000); writeint_last_row(6, sec); // write second byte timestamp = timestamp % 1000; millisec = timestamp; writeint_last_row(10, millisec); // write millisecond byte writeint_last_row(10, timeinterval); // write time interval timestamp = time_pre; // System.out.println("Changing timestamp******** " + timestamp + "| residule ==" + // residule + "\n " ); for (i = quote * (packetbytes_length / 2 - 2); i < ((quote) * (packetbytes_length / 2 - 2) + residule); i++) { writeint_last_row(16, decodedunfoldedsamples[i]); } // reset variables for next packet read decodepacketbitpointer = 0; decodepacketbytepointer = 0; // Write to a file with the timestamp and numdecodedsamples /*descript_string += time_s; descript_string += " "; descript_string += residule; descript_string += "\r\n"; for (int m=0; m<descript_string.length();++m){ outstream.write((byte)descript_string.charAt(m)); } descript_string = "";*/ } outstream.close(); } catch (IOException e) { } // return 1; } }
public static void main(String[] arg) { if (arg.length != 2) { System.err.println("usage: java ScpFrom user@remotehost:file1 file2"); System.exit(-1); } FileOutputStream fos = null; try { String user = arg[0].substring(0, arg[0].indexOf('@')); arg[0] = arg[0].substring(arg[0].indexOf('@') + 1); String host = arg[0].substring(0, arg[0].indexOf(':')); String rfile = arg[0].substring(arg[0].indexOf(':') + 1); String lfile = arg[1]; String prefix = null; if (new File(lfile).isDirectory()) { prefix = lfile + File.separator; } JSch jsch = new JSch(); Session session = jsch.getSession(user, host, 22); // username and password will be given via UserInfo interface. UserInfo ui = new MyUserInfo(); session.setUserInfo(ui); session.connect(); // exec 'scp -f rfile' remotely String command = "scp -f " + rfile; Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); // get I/O streams for remote scp OutputStream out = channel.getOutputStream(); InputStream in = channel.getInputStream(); channel.connect(); byte[] buf = new byte[1024]; // send '\0' buf[0] = 0; out.write(buf, 0, 1); out.flush(); while (true) { int c = checkAck(in); if (c != 'C') { break; } // read '0644 ' in.read(buf, 0, 5); long filesize = 0L; while (true) { if (in.read(buf, 0, 1) < 0) { // error break; } if (buf[0] == ' ') break; filesize = filesize * 10L + (long) (buf[0] - '0'); } String file = null; for (int i = 0; ; i++) { in.read(buf, i, 1); if (buf[i] == (byte) 0x0a) { file = new String(buf, 0, i); break; } } // System.out.println("filesize="+filesize+", file="+file); // send '\0' buf[0] = 0; out.write(buf, 0, 1); out.flush(); // read a content of lfile fos = new FileOutputStream(prefix == null ? lfile : prefix + file); int foo; while (true) { if (buf.length < filesize) foo = buf.length; else foo = (int) filesize; foo = in.read(buf, 0, foo); if (foo < 0) { // error break; } fos.write(buf, 0, foo); filesize -= foo; if (filesize == 0L) break; } fos.close(); fos = null; if (checkAck(in) != 0) { System.exit(0); } // send '\0' buf[0] = 0; out.write(buf, 0, 1); out.flush(); } session.disconnect(); System.exit(0); } catch (Exception e) { System.out.println(e); try { if (fos != null) fos.close(); } catch (Exception ee) { } } }