Update of /cvsroot/metamorphosis/ruper2/src/java/core/org/krysalis/ruper2/util/io
In directory sc8-pr-cvs1:/tmp/cvs-serv25339/src/java/core/org/krysalis/ruper2/util/io
Modified Files:
FileUtils.java ResolvedFile.java
Added Files:
IOUtils.java
Log Message:
Opps, did it for HTTP input but not File input. Added that.
--- NEW FILE: IOUtils.java ---
package org.krysalis.ruper2.util.io;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class IOUtils {
public static void transfer(InputStream input, OutputStream output)
throws IOException {
// Transfer the bytes in blocks..
byte b[] = new byte[1000];
int numRead = -1;
do {
numRead = input.read(b);
if (numRead != -1)
output.write(b, 0, numRead);
}
while (numRead != -1);
}
public static void close(InputStream stream) {
if (null != stream) {
try {
stream.close();
}
catch (Exception e) {
}
}
}
public static void close(OutputStream stream) {
if (null != stream) {
try {
stream.close();
}
catch (Exception e) {
}
}
}
}
Index: FileUtils.java
===================================================================
RCS file: /cvsroot/metamorphosis/ruper2/src/java/core/org/krysalis/ruper2/util/io/FileUtils.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** FileUtils.java 29 Sep 2003 18:09:24 -0000 1.1
--- FileUtils.java 9 Oct 2003 21:14:57 -0000 1.2
***************
*** 2,5 ****
--- 2,8 ----
import java.io.File;
+ import java.io.FileInputStream;
+ import java.io.FileOutputStream;
+ import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
***************
*** 7,85 ****
import java.text.StringCharacterIterator;
! public class FileUtils {
! // for toURI
! private static boolean[] isSpecial = new boolean[256];
! private static char[] escapedChar1 = new char[256];
! private static char[] escapedChar2 = new char[256];
! // stolen from FilePathToURI of the Xerces-J team
! static {
! for (int i = 0; i <= 0x20; i++) {
! isSpecial[i] = true;
! escapedChar1[i] = Character.forDigit(i >> 4, 16);
! escapedChar2[i] = Character.forDigit(i & 0xf, 16);
! }
! isSpecial[0x7f] = true;
! escapedChar1[0x7f] = '7';
! escapedChar2[0x7f] = 'F';
! char[] escChs = {'<', '>', '#', '%', '"', '{', '}',
! '|', '\\', '^', '~', '[', ']', '`'};
! int len = escChs.length;
! char ch;
! for (int i = 0; i < len; i++) {
! ch = escChs[i];
! isSpecial[ch] = true;
! escapedChar1[ch] = Character.forDigit(ch >> 4, 16);
! escapedChar2[ch] = Character.forDigit(ch & 0xf, 16);
! }
! }
! /**
! * Get the URL for a file taking into account # characters
! *
! * @param file the file whose URL representation is required.
! * @return The FileURL value
! * @throws MalformedURLException if the URL representation cannot be
! * formed.
! */
! public static URL getFileURL(File file) throws MalformedURLException {
! return new URL(toURI(file.getAbsolutePath()));
! }
! public static String toURI(String path) {
! boolean isDir = (new File(path)).isDirectory();
! StringBuffer sb = new StringBuffer("file:");
! sb.append("//");
! // add an extra slash for filesystems with drive-specifiers
! if (!path.startsWith(File.separator)) {
! sb.append("/");
! }
! path = path.replace('\\', '/');
! CharacterIterator iter = new StringCharacterIterator(path);
! for (char c = iter.first(); c != CharacterIterator.DONE;
! c = iter.next()) {
! if (isSpecial[c]) {
! sb.append('%');
! sb.append(escapedChar1[c]);
! sb.append(escapedChar2[c]);
! } else {
! sb.append(c);
! }
! }
! if (isDir && !path.endsWith("/")) {
! sb.append('/');
! }
! return sb.toString();
! }
}
-
--- 10,120 ----
import java.text.StringCharacterIterator;
+ public class FileUtils {
+ // for toURI
+ private static boolean[] isSpecial = new boolean[256];
+ private static char[] escapedChar1 = new char[256];
+ private static char[] escapedChar2 = new char[256];
+ // stolen from FilePathToURI of the Xerces-J team
+ static {
+ for (int i = 0; i <= 0x20; i++) {
+ isSpecial[i] = true;
+ escapedChar1[i] = Character.forDigit(i >> 4, 16);
+ escapedChar2[i] = Character.forDigit(i & 0xf, 16);
+ }
+ isSpecial[0x7f] = true;
+ escapedChar1[0x7f] = '7';
+ escapedChar2[0x7f] = 'F';
+ char[] escChs =
+ {
+ '<',
+ '>',
+ '#',
+ '%',
+ '"',
+ '{',
+ '}',
+ '|',
+ '\\',
+ '^',
+ '~',
+ '[',
+ ']',
+ '`' };
+ int len = escChs.length;
+ char ch;
+ for (int i = 0; i < len; i++) {
+ ch = escChs[i];
+ isSpecial[ch] = true;
+ escapedChar1[ch] = Character.forDigit(ch >> 4, 16);
+ escapedChar2[ch] = Character.forDigit(ch & 0xf, 16);
+ }
+ }
! /**
! * Get the URL for a file taking into account # characters
! *
! * @param file the file whose URL representation is required.
! * @return The FileURL value
! * @throws MalformedURLException if the URL representation cannot be
! * formed.
! */
! public static URL getFileURL(File file) throws MalformedURLException {
! return new URL(toURI(file.getAbsolutePath()));
! }
! public static String toURI(String path) {
! boolean isDir = (new File(path)).isDirectory();
+ StringBuffer sb = new StringBuffer("file:");
! sb.append("//");
! // add an extra slash for filesystems with drive-specifiers
! if (!path.startsWith(File.separator)) {
! sb.append("/");
! }
! path = path.replace('\\', '/');
! CharacterIterator iter = new StringCharacterIterator(path);
! for (char c = iter.first();
! c != CharacterIterator.DONE;
! c = iter.next()) {
! if (isSpecial[c]) {
! sb.append('%');
! sb.append(escapedChar1[c]);
! sb.append(escapedChar2[c]);
! }
! else {
! sb.append(c);
! }
! }
! if (isDir && !path.endsWith("/")) {
! sb.append('/');
! }
! return sb.toString();
! }
! public static void transfer(File inputFile, File outputFile) throws IOException {
! FileInputStream input = null;
! try {
! input = new FileInputStream(inputFile);
! FileOutputStream output = null;
! try {
! output = new FileOutputStream(outputFile);
+ IOUtils.transfer(input, output);
+ }
+ catch (Exception ie) {
+ IOUtils.close(output);
+ }
+ }
+ catch (Exception ie) {
+ IOUtils.close(input);
+ }
+ }
}
Index: ResolvedFile.java
===================================================================
RCS file: /cvsroot/metamorphosis/ruper2/src/java/core/org/krysalis/ruper2/util/io/ResolvedFile.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** ResolvedFile.java 29 Sep 2003 18:09:24 -0000 1.1
--- ResolvedFile.java 9 Oct 2003 21:14:57 -0000 1.2
***************
*** 26,34 ****
}
! public static File resolve(Object file) {
return resolve(SystemUtils.getCWD(), file);
}
! public static File resolve(Object m_resolverContext, Object file) {
// Previously resolved within this context...
--- 26,34 ----
}
! public static ResolvedFile resolve(Object file) {
return resolve(SystemUtils.getCWD(), file);
}
! public static ResolvedFile resolve(Object m_resolverContext, Object file) {
// Previously resolved within this context...
***************
*** 37,41 ****
.m_resolverContext
.equals(m_resolverContext)))
! return (File) file;
// Extract a 'path' (local or absolute) from this thing
--- 37,41 ----
.m_resolverContext
.equals(m_resolverContext)))
! return (ResolvedFile) file;
// Extract a 'path' (local or absolute) from this thing
-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
SourceForge.net hosts over 70,000 Open Source Projects.
See the people who have HELPED US provide better services:
Click here: http://sourceforge.net/supporters.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.