[tcljava-dev] file copy and directories
Brought to you by:
mdejong
From: Raffaele S. <ra...@ar...> - 2001-12-08 09:50:13
|
Here are the changes that add support for directories to "file copy". I may be missing some check and some proper error message, but they work. -- Raffaele Index: src/jacl/tcl/lang/FileCmd.java =================================================================== RCS file: /cvsroot/tcljava/tcljava/src/jacl/tcl/lang/FileCmd.java,v retrieving revision 1.5 diff -b -r1.5 FileCmd.java 99a100,101 > private static final int COPYBUFFER_SIZE = 4096; > 1088a1091 > if (targetFileObj.exists()) { 1100a1104 > } 1109a1114,1116 > } else > if (sourceFileObj.isDirectory()) { > copyDirectory(interp, sourceName, targetName, force); 1114,1115c1121,1122 < // Read source to "cbuf" char array one line at a time. < // For each line, Write cbuf to target. --- > // Read source to "buf" byte array one block at a time. > // For each block, Write buf to target. 1117,1123c1124,1129 < 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); --- > BufferedInputStream reader = > new BufferedInputStream(new FileInputStream(sourceFileObj)); > BufferedOutputStream writer = > new BufferedOutputStream(new FileOutputStream(targetFileObj)); > byte buf[] = new byte[COPYBUFFER_SIZE]; > int numChars = reader.read(buf, 0, COPYBUFFER_SIZE); 1125,1127c1131,1132 < writer.write(cbuf, currentIndex, numChars); < currentIndex += 256; < numChars = reader.read(cbuf, currentIndex, 256); --- > writer.write(buf, 0, numChars); > numChars = reader.read(buf, 0, COPYBUFFER_SIZE); 1133a1139,1207 > } > } > > > /* > *----------------------------------------------------------------------------- > * > * copyDirectory -- > * > * Create the target directory and copy each file in the source > * directory. > * > * If there are any subdirectories, copyDirectory will be called > * recursively (by copyRenameOneFile). > * > * Results: > * None. > * > * Side effects: > * See user documentation. > * > *----------------------------------------------------------------------------- > */ > > private static void > copyDirectory( > Interp interp, // Current interpreter. > String sourceName, // Name of source file. > String targetName, // Name of target file. > boolean force) // Flag tells whether to overwrite. > throws > TclException // Thrown as a result of bad user input. > { > boolean madeDir = false; > > // we already checked this before > File sourceDirObj = FileUtil.getNewFileObj(interp, sourceName); > File targetDirObj = FileUtil.getNewFileObj(interp, targetName); > > // the output directory doesn't exist > try { > madeDir = targetDirObj.mkdir(); > if (!madeDir) { > madeDir = targetDirObj.mkdirs(); > } > } catch (SecurityException e) { > throw new TclException(interp, e.getMessage()); > } > if (!madeDir) { > throw new TclPosixException(interp, TclPosixException.EACCES, true, > "can't create directory \"" + targetName + > "\": best guess at reason"); > } > > // scan the source directory and copy each file > String fileList[] = sourceDirObj.list(); > for (int i = 0; i < fileList.length; i++) { > > TclObject joinArrayObj[] = new TclObject[2]; > > joinArrayObj[0] = TclString.newInstance(sourceName); > joinArrayObj[1] = TclString.newInstance(fileList[i]); > String sourcePath = FileUtil.joinPath(interp, joinArrayObj, 0, 2); > > joinArrayObj[0] = TclString.newInstance(targetName); > joinArrayObj[1] = TclString.newInstance(fileList[i]); > String targetPath = FileUtil.joinPath(interp, joinArrayObj, 0, 2); > > copyRenameOneFile(interp, sourcePath, targetPath, true, force); |