Author: rob.stryker(a)jboss.com
Date: 2012-01-30 02:15:34 -0500 (Mon, 30 Jan 2012)
New Revision: 38260
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/util/internal/FileUtils.java
Log:
internal utils missed a commit ?
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/util/internal/FileUtils.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/util/internal/FileUtils.java 2012-01-30
01:31:56 UTC (rev 38259)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/util/internal/FileUtils.java 2012-01-30
07:15:34 UTC (rev 38260)
@@ -11,12 +11,18 @@
package org.jboss.ide.eclipse.as.core.util.internal;
import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
+
/**
* Various util methods that allow to read and write to files.
*
@@ -26,7 +32,6 @@
public class FileUtils {
private static final int BUFFER = 65536;
- private static byte[] buffer = new byte[BUFFER];
public static void writeTo(InputStream in, String fileName) throws IOException {
writeTo(in, new File(fileName));
@@ -45,10 +50,86 @@
}
public static void writeTo(InputStream in, OutputStream out) throws IOException {
+ byte[] buffer = new byte[BUFFER];
int avail = in.read(buffer);
while (avail > 0) {
out.write(buffer, 0, avail);
avail = in.read(buffer);
}
}
+
+ public static void setContents(File file, String contents) throws IOException,
CoreException {
+ byte[] buffer = new byte[BUFFER];
+ InputStream in = new ByteArrayInputStream(contents.getBytes());
+ OutputStream out = null;
+ try {
+ out = new BufferedOutputStream(new FileOutputStream(file));
+ int avail = in.read(buffer);
+ while (avail > 0) {
+ out.write(buffer, 0, avail);
+ avail = in.read(buffer);
+ }
+ } finally {
+ if (out != null) {
+ out.close();
+ }
+ }
+ }
+
+ public static String getContents(IFile file) throws IOException, CoreException {
+ return getContents(file.getLocation().toFile());
+ }
+
+ public static String getContents(File aFile) throws IOException {
+ return new String(getBytesFromFile(aFile));
+ }
+
+ public static byte[] getBytesFromFile(File file) throws IOException {
+ InputStream is = new FileInputStream(file);
+ byte[] bytes = new byte[(int)file.length()];
+ int offset = 0;
+ int numRead = 0;
+ while (offset < bytes.length
+ && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0)
{
+ offset += numRead;
+ }
+ is.close();
+ return bytes;
+ }
+
+ public static void setContents(IFile file, int val) throws IOException , CoreException{
+ setContents(file, "" + val); //$NON-NLS-1$
+ }
+
+ public static void setContents(IFile file, String val) throws IOException ,
CoreException{
+ if( !file.exists())
+ file.create(new ByteArrayInputStream((val).getBytes()), false, null);
+ else
+ file.setContents(new ByteArrayInputStream((val).getBytes()), false, false, new
NullProgressMonitor());
+ try {
+ Thread.sleep(2000);
+ } catch( InterruptedException ie) {}
+ //JobUtils.waitForIdle();
+ }
+
+ public static int countFiles(File root) {
+ int count = 0;
+ if( !root.isDirectory() )
+ return 1;
+ File[] children = root.listFiles();
+ for( int i = 0; i < children.length; i++ )
+ count += countFiles(children[i]);
+ return count;
+ }
+
+ public static int countAllResources(File root) {
+ int count = 0;
+ if( !root.isDirectory() )
+ return 1;
+ File[] children = root.listFiles();
+ for( int i = 0; i < children.length; i++ )
+ count += countAllResources(children[i]);
+ return 1 + count;
+ }
+
}
Show replies by date