Re: [tcljava-dev] PATCH: Fix for file copy command
Brought to you by:
mdejong
From: Mo D. <su...@ba...> - 2002-04-12 19:23:44
|
On 27 Mar 2002 07:12:25 -0500 Shawn Boyce <sh...@qc...> wrote: > The file copy command does not work. e.g. > % file copy a b > error copying: null > > Attached is the patch for FileCmd.java Now that you mention it, I don't even see why the file data is being treated as encoded text here. How about this patch instead? It copies the data as binary while also including your fix for the array bounds. cheers Mo 2002-04-12 Shawn Boyce <sh...@qc...> * src/jacl/tcl/lang/FileCmd.java (copyRenameOneFile): Treat data copied via 'file copy' as binary. Fix array bound bug that was causing the copy command to fail. Index: src/jacl/tcl/lang/FileCmd.java =================================================================== RCS file: /cvsroot/tcljava/tcljava/src/jacl/tcl/lang/FileCmd.java,v retrieving revision 1.6 diff -u -r1.6 FileCmd.java --- src/jacl/tcl/lang/FileCmd.java 12 Apr 2002 18:13:37 -0000 1.6 +++ src/jacl/tcl/lang/FileCmd.java 12 Apr 2002 19:18:05 -0000 @@ -1161,24 +1164,21 @@ // Perform the copy procedure. try { - // Read source to "cbuf" char array one line at a time. - // For each line, Write cbuf to target. - - BufferedReader reader = - new BufferedReader(new FileReader(sourceFileObj)); - BufferedWriter writer = - new BufferedWriter(new FileWriter(targetFileObj)); - char cbuf[] = new char[256]; - int currentIndex = 0; - int numChars = reader.read(cbuf, 0, 256); - while (numChars != -1) { - writer.write(cbuf, currentIndex, numChars); - currentIndex += 256; - numChars = reader.read(cbuf, currentIndex, 256); - } - reader.close(); - writer.close(); - } catch (Exception e) { + BufferedInputStream bin = new BufferedInputStream( + new FileInputStream(sourceFileObj)); + BufferedOutputStream bout = new BufferedOutputStream( + new FileOutputStream(targetFileObj)); + + final int bsize = 1024; + byte[] buff = new byte[bsize]; + int numChars = bin.read(buff, 0, bsize); + while (numChars != -1) { + bout.write(buff, 0, numChars); + numChars = bin.read(buff, 0, bsize); + } + bin.close(); + bout.close(); + } catch (IOException e) { throw new TclException(interp, "error copying: " + e.getMessage()); } } |