001: package snow.utils.storage;
002:
003: import snow.utils.gui.ProgressModalDialog;
004: import java.text.DecimalFormat;
005: import snow.Basics;
006: import java.io.*;
007: import java.util.*;
008: import java.util.zip.*;
009: import java.util.jar.*;
010:
011: /** All file access should be done from here
012: */
013: public final class FileUtils {
014: private FileUtils() {
015: }
016:
017: public static StorageVector loadVectorFromFile(File file)
018: throws Exception {
019: return loadVectorFromFile(file, false);
020: }
021:
022: public static StorageVector loadVectorFromFile(File file,
023: boolean faultTolerant) throws Exception {
024: if (!file.exists())
025: throw new Exception("File " + file.getAbsolutePath()
026: + " doesn't exist");
027: FileInputStream fis = null;
028: DataInputStream dis = null;
029: StorageVector v = new StorageVector();
030: try {
031: fis = new FileInputStream(file);
032: //fis.getChannel().lock();
033: dis = new DataInputStream(fis);
034: VectorUtils.streamToVector(dis, v);
035: dis.close();
036: return v;
037: } catch (Exception e) {
038: if (faultTolerant) {
039: e.printStackTrace();
040: return v;
041: }
042:
043: throw e;
044: } finally {
045: closeIgnoringExceptions(fis);
046: }
047: }
048:
049: public static void gunzip(File in, File out) throws Exception {
050: FileInputStream fis = null;
051: GZIPInputStream zis = null;
052: FileOutputStream fos = null;
053: try {
054: fis = new FileInputStream(in);
055: zis = new GZIPInputStream(fis);
056: fos = new FileOutputStream(out);
057: byte[] buf = new byte[512];
058: int read = -1;
059: while ((read = zis.read(buf)) != -1) {
060: fos.write(buf, 0, read);
061: }
062: fos.flush();
063: } catch (Exception e) {
064: throw e;
065: } finally {
066: closeIgnoringExceptions(fis);
067: closeIgnoringExceptions(fos);
068: }
069: }
070:
071: /** Works... but not perfect, not "on the fly". Needs a fully unpack.
072: */
073: public static StorageVector loadZippedVectorFromFile3(File file)
074: throws Exception {
075: if (!file.exists())
076: throw new Exception("File " + file.getAbsolutePath()
077: + " doesn't exist");
078:
079: File tf = File.createTempFile("tide", ".temp");
080: tf.deleteOnExit();
081: gunzip(file, tf);
082: try {
083: return loadVectorFromFile(tf);
084: } catch (Exception e) {
085: throw e;
086: } finally {
087: tf.delete();
088: }
089:
090: }
091:
092: /** Loads the vector from the inputstream.
093: * Closes the input stream at end
094: */
095: public static StorageVector loadVector(InputStream is)
096: throws Exception {
097: DataInputStream dis = null;
098: StorageVector v = new StorageVector();
099: try {
100: dis = new DataInputStream(is);
101: VectorUtils.streamToVector(dis, v);
102: dis.close();
103: return v;
104: } catch (Exception e) {
105: throw e;
106: } finally {
107: closeIgnoringExceptions(dis);
108: }
109: }
110:
111: /**
112: Caution: the stream is not closed, this allow you to read several vectory from the stream
113: a header and a body, for example.
114: */
115: public static StorageVector loadVectorWithoutClose(InputStream is)
116: throws Exception {
117: DataInputStream dis = null;
118: StorageVector v = new StorageVector();
119: try {
120: dis = new DataInputStream(is);
121: VectorUtils.streamToVector(dis, v);
122: return v;
123: } catch (Exception e) {
124: throw e;
125: }
126: }
127:
128: /** creates the folders if needed...
129: */
130: public static void saveVectorToFile(File file, List<Object> v)
131: throws Exception {
132: if (file.exists()) {
133: // delete
134: if (!file.delete()) {
135: System.out.println("File " + file.getAbsolutePath()
136: + " already exists and cannot be deleted");
137: }
138: }
139:
140: // create the dirs...
141: File parent = file.getParentFile();
142: if (parent != null && !parent.exists()) {
143: if (!parent.mkdirs()) {
144: throw new Exception(
145: "Cannot create the directories for " + file);
146: }
147: }
148:
149: FileOutputStream fos = null;
150: DataOutputStream dos = null;
151: try {
152: fos = new FileOutputStream(file);
153: fos.getChannel().lock();
154:
155: dos = new DataOutputStream(fos);
156: VectorUtils.vectorToStream(dos, v);
157: } catch (Exception e) {
158: throw e;
159: } finally {
160: closeIgnoringExceptions(fos);
161: }
162:
163: }
164:
165: public static void saveZippedVectorToFile(File file, List<Object> v)
166: throws Exception {
167: if (file.exists()) {
168: // delete
169: if (!file.delete()) {
170: System.out.println("File " + file.getAbsolutePath()
171: + " already exists and cannot be deleted");
172: }
173: }
174: FileOutputStream fos = null;
175: GZIPOutputStream gzo = null;
176: DataOutputStream dos = null;
177: try {
178: fos = new FileOutputStream(file);
179: gzo = new GZIPOutputStream(fos);
180: dos = new DataOutputStream(gzo);
181:
182: VectorUtils.vectorToStream(dos, v);
183: dos.flush();
184: gzo.finish();
185: gzo.flush();
186: gzo.close();
187: //fos.close();
188: } catch (Exception e) {
189: throw e;
190: } finally {
191: closeIgnoringExceptions(fos);
192: }
193:
194: }
195:
196: /*test
197: public static void main(String[] arguments) throws Exception
198: {
199:
200: //System.out.println("AAA"+loadVectorFromFile(new File("c:/temp/aaa")));
201:
202: File f = new File("c:/temp/aaa.txt.gz");
203: List<Object> tl = new ArrayList<Object>(Arrays.asList("Hello world"));
204: saveZippedVectorToFile(f, tl);
205:
206: //System.out.println("zf="+Arrays.toString(getFileByteContent(f)));
207:
208: System.out.println(""+loadZippedVectorFromFile3(f));
209: }*/
210:
211: /** works fine. (but passes through an external file).
212: *
213: public static void test() throws Exception
214: {
215: File gzfile = new File("c:/temp/abc.txt.gz");
216: GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(gzfile));
217:
218: byte[] bh = "Hello world !".getBytes();
219: System.out.println(""+Arrays.toString(bh));
220: gzos.write(bh);
221: gzos.finish();
222: gzos.flush();
223: gzos.close();
224:
225: GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(gzfile));
226: ByteArrayOutputStream baos = new ByteArrayOutputStream();
227: byte[] buf = new byte[256];
228: int read = 0;
229: while((read=gzis.read(buf))!=-1)
230: {
231: baos.write(buf,0,read);
232: }
233: System.out.println("bh2"+ Arrays.toString(baos.toByteArray()));
234: }*/
235:
236: public static void closeIgnoringExceptions(Closeable c) {
237: if (c != null)
238: try {
239: c.close();
240: } catch (Exception ee) {
241: Basics.ignore(ee);
242: }
243: }
244:
245: public static void closeIgnoringExceptions(JarFile c) {
246: if (c != null)
247: try {
248: c.close();
249: } catch (Exception ee) {
250: Basics.ignore(ee);
251: }
252: }
253:
254: public static void closeIgnoringExceptions(ZipFile c) {
255: if (c != null)
256: try {
257: c.close();
258: } catch (Exception ee) {
259: Basics.ignore(ee);
260: }
261: }
262:
263: public static void addToZip(final ZipOutputStream zos, File f,
264: String relName) throws Exception {
265: FileInputStream fis = null;
266: try {
267: ZipEntry ze = new ZipEntry(relName);
268: ze.setTime(f.lastModified());
269: zos.putNextEntry(ze);
270: fis = new FileInputStream(f);
271: byte[] buffer = new byte[256];
272: int read = 0;
273: while ((read = fis.read(buffer)) != -1) {
274: zos.write(buffer, 0, read);
275: }
276: zos.closeEntry();
277: } catch (Exception e) {
278: throw e;
279: } finally {
280: closeIgnoringExceptions(fis);
281: }
282: }
283:
284: /**
285: * better char encoding (correct charset).
286: * can also handle attributes (readonly) ... NOT made.
287: */
288: public static void addToZip(
289: final org.apache.tools.zip.ZipOutputStream zos, File f,
290: String relName) throws Exception {
291: FileInputStream fis = null;
292: try {
293: org.apache.tools.zip.ZipEntry ze = new org.apache.tools.zip.ZipEntry(
294: relName);
295: ze.setTime(f.lastModified());
296: zos.putNextEntry(ze);
297: fis = new FileInputStream(f);
298: byte[] buffer = new byte[256];
299: int read = 0;
300: while ((read = fis.read(buffer)) != -1) {
301: zos.write(buffer, 0, read);
302: }
303: zos.closeEntry();
304: } catch (Exception e) {
305: throw e;
306: } finally {
307: closeIgnoringExceptions(fis);
308: }
309: }
310:
311: /** Double buffered extracter, pmd used for cancel.
312: */
313: public static void extractZipFile(final File file,
314: final File destFold, final ProgressModalDialog pmd)
315: throws Exception {
316: final FileInputStream fis = new FileInputStream(file);
317: final BufferedInputStream bis = new BufferedInputStream(fis);
318: final ZipInputStream zis = new ZipInputStream(bis); // less mem usage than ZipFile, but unkown entries count !
319:
320: try {
321: ZipEntry ze = zis.getNextEntry();
322: byte[] buf = new byte[1024];
323: int rd = 0;
324: while (ze != null) {
325: if (pmd.getWasCancelled())
326: throw new Exception("Cancelled");
327: // long size = ze.getSize();
328: pmd.setProgressComment("Unpacking " + ze.getName());
329: File df = new File(destFold, ze.getName());
330: //System.out.println(""+df);
331: if (ze.isDirectory()) {
332: df.mkdirs();
333: zis.closeEntry();
334: ze = zis.getNextEntry();
335: continue;
336: }
337:
338: if (!df.getParentFile().exists()) {
339: df.getParentFile().mkdirs();
340: }
341:
342: FileOutputStream fos = new FileOutputStream(df);
343: BufferedOutputStream bos = new BufferedOutputStream(fos);
344:
345: while ((rd = zis.read(buf)) != -1) {
346: bos.write(buf, 0, rd);
347: }
348: bos.flush();
349: bos.close();
350: df.setLastModified(ze.getTime());
351:
352: zis.closeEntry();
353: ze = zis.getNextEntry();
354: }
355: } finally {
356: FileUtils.closeIgnoringExceptions(zis);
357: }
358:
359: }
360:
361: /** With / as separator and a / at the end for directories
362: * CAUTION: case is NOT canonicalized. call first getCanonicalFileWithCase() on the file !
363: */
364: public static String getCanonicalName(File f) {
365: String n = f.getAbsolutePath().replace('\\', '/');
366: if (f.isDirectory()) {
367: if (!n.endsWith("/")) {
368: n += "/";
369: }
370: }
371: return n;
372: }
373:
374: /** just calls the File.getCanonicalFile().
375: * The cases are corrected and ".." are "resolved".
376: * Should be called on each file object that is compared !
377: * Be very careful, E: and e: are denoting the same path on windows, but they string are not "equals"
378: */
379: public static File getCanonicalFileWithCase(File f) {
380: try {
381: return f.getCanonicalFile();
382: } catch (Exception ign) {
383: return f;
384: }
385: }
386:
387: public static String getFileStringContent(File file)
388: throws Exception {
389: if (!file.exists())
390: throw new Exception("File not existing: " + file);
391: StringBuilder sb = new StringBuilder();
392: InputStreamReader ir = null;
393: FileInputStream fis = null;
394: try {
395: fis = new FileInputStream(file);
396: ir = new InputStreamReader(fis);
397:
398: char[] cbuf = new char[256];
399: int read = -1;
400: while ((read = ir.read(cbuf)) != -1) {
401: sb.append(cbuf, 0, read);
402: }
403: return sb.toString();
404: } catch (Exception e) {
405: throw e;
406: } finally {
407: closeIgnoringExceptions(fis);
408: }
409: }
410:
411: public static String getStringContent(InputStream is)
412: throws Exception {
413: StringBuilder sb = new StringBuilder();
414: InputStreamReader ir = null;
415:
416: try {
417: ir = new InputStreamReader(is);
418:
419: char[] cbuf = new char[256];
420: int read;
421: while ((read = ir.read(cbuf)) != -1) {
422: sb.append(cbuf, 0, read);
423: }
424: return sb.toString();
425: } catch (Exception e) {
426: throw e;
427: } finally {
428: closeIgnoringExceptions(is);
429: }
430: }
431:
432: public static byte[] getFileByteContent(File file) throws Exception {
433: ByteArrayOutputStream baos = new ByteArrayOutputStream();
434: FileInputStream fis = null;
435: try {
436: fis = new FileInputStream(file);
437: byte[] buf = new byte[256];
438: int read = -1;
439: while ((read = fis.read(buf)) != -1) {
440: baos.write(buf, 0, read);
441: }
442: baos.flush();
443: baos.close();
444: return baos.toByteArray();
445: } catch (Exception e) {
446: throw e;
447: } finally {
448: closeIgnoringExceptions(fis);
449: }
450: }
451:
452: public static void saveToFile(String cont, File file)
453: throws Exception {
454: FileOutputStream fos = null;
455: try {
456: fos = new FileOutputStream(file);
457: PrintWriter pw = new PrintWriter(fos);
458: pw.write(cont);
459: pw.flush(); // IMPORTANT !
460: } catch (Exception e) {
461: throw e;
462: } finally {
463: closeIgnoringExceptions(fos);
464: }
465: }
466:
467: public static void copy(File source, File dest) throws Exception {
468: writeToFile(new FileInputStream(source), dest);
469: dest.setLastModified(source.lastModified());
470: }
471:
472: /** @param is is closed in all situations.
473: */
474: public static void writeToFile(InputStream is, File file)
475: throws Exception {
476: File parent = file.getParentFile();
477: if (parent != null && !parent.exists()) {
478: parent.mkdirs();
479: }
480:
481: FileOutputStream fos = null;
482: try {
483: fos = new FileOutputStream(file);
484: byte[] buf = new byte[256];
485: int read = -1;
486: while ((read = is.read(buf)) != -1) {
487: fos.write(buf, 0, read);
488: }
489: fos.flush();
490: } catch (Exception e) {
491: throw e;
492: } finally {
493: closeIgnoringExceptions(fos);
494: closeIgnoringExceptions(is);
495: }
496: }
497:
498: public static void saveBytesToFile(byte[] cont, File file)
499: throws Exception {
500: FileOutputStream fos = null;
501: try {
502: fos = new FileOutputStream(file);
503: fos.write(cont);
504: fos.flush();
505: } catch (Exception e) {
506: throw e;
507: } finally {
508: closeIgnoringExceptions(fos);
509: }
510: }
511:
512: public static long getSizeIncludingSubFiles(File root) {
513: if (root.isFile())
514: return root.length();
515:
516: List<File> all = new ArrayList<File>();
517: getAllFilesRecurse(root, all, false, false);
518:
519: long tot = 0;
520: for (File f : all) {
521: tot += f.length();
522: }
523: return tot;
524: }
525:
526: /** All files but not the files ending with the given names
527: */
528: public static List<File> getAllResourceFilesRecurse(
529: final File root, final List<String> ignoredEndingsLow) {
530: List<File> allFiles = new ArrayList<File>();
531: getAllFilesRecurse(root, allFiles, false, true); // ignore dirs starting with "."
532:
533: // remove
534: if (ignoredEndingsLow != null && ignoredEndingsLow.size() > 0) {
535: for (int i = allFiles.size() - 1; i >= 0; i--) {
536: File fi = allFiles.get(i);
537: le: for (String end : ignoredEndingsLow) {
538: if (fi.getName().toLowerCase().endsWith(end)) {
539: allFiles.remove(fi);
540: break le;
541: }
542: }
543: //if(fi.getName().toLowerCase().endsWith(".java")) allFiles.remove(fi);
544: }
545: }
546:
547: return allFiles;
548: }
549:
550: /** if withDirectories, append also the directories
551: * Remark: the root is not added.
552: * settings folders, starting with "." are also ignored.
553: * in case of folders inclusion, folder are present first to their content, this
554: * may be useful when creating a tree...
555: * SLOW: getAllJavaFilesRecurse is quicker.
556: */
557: public static void getAllFilesRecurse(final File root,
558: final List<File> allFiles, final boolean withDirectories,
559: final boolean ignoringDirsStartingWithDot) {
560: if (root == null)
561: return;
562: if (!root.exists())
563: return;
564:
565: for (File fi : root.listFiles()) {
566: if (fi.isDirectory()) {
567: if (ignoringDirsStartingWithDot
568: && fi.getName().startsWith(".")) // ignores settings folders
569: {
570: continue;
571: }
572:
573: if (withDirectories) {
574: allFiles.add(fi);
575: }
576: getAllFilesRecurse(fi, allFiles, withDirectories,
577: ignoringDirsStartingWithDot);
578: } else {
579: allFiles.add(fi);
580: }
581: }
582: }
583:
584: /** Version with limited recursion depth.
585: * maxDepth=0 => only files.
586: */
587: public static void getAllFilesRecurseLim(final File root,
588: final List<File> allFiles, final boolean withDirectories,
589: final boolean ignoringDirsStartingWithDot, int maxDepth,
590: final ProgressModalDialog pmd) {
591: if (pmd != null && pmd.getWasCancelled())
592: return;
593:
594: if (maxDepth < 0)
595: return;
596:
597: if (root == null)
598: return;
599: if (!root.exists())
600: return;
601:
602: File[] lfs = root.listFiles();
603: if (lfs != null) // for example proteced system folders in windows
604: {
605: if (pmd != null)
606: pmd.setProgressCommentAndKeepIndeterminate("analysing "
607: + root + ", " + allFiles.size()
608: + " files so far...");
609: for (File fi : lfs) {
610: if (fi.isDirectory()) {
611: if (ignoringDirsStartingWithDot
612: && fi.getName().startsWith(".")) // ignores settings folders
613: {
614: continue;
615: }
616:
617: if (withDirectories) {
618: allFiles.add(fi);
619: }
620: getAllFilesRecurseLim(fi, allFiles,
621: withDirectories,
622: ignoringDirsStartingWithDot, maxDepth - 1,
623: pmd);
624: } else {
625: allFiles.add(fi);
626: }
627: }
628: }
629: }
630:
631: /** Accepts dirs and java files. Using this filter is about 5 times quicker than filtering all files got with File.listFiles(). */
632: static final class JavaFileNameFilter implements FileFilter {
633: public boolean accept(final File f) {
634: if (f.isDirectory())
635: return true;
636: if (f.getName().toLowerCase().endsWith(".java"))
637: return true;
638: return false;
639: }
640: }
641:
642: /** Accepts dirs and class files. Using this filter is about 5 times quicker than filtering all files got with File.listFiles(). */
643: static final class ClassFileNameFilter implements FileFilter {
644: public boolean accept(final File f) {
645: if (f.isDirectory())
646: return true;
647: if (f.getName().toLowerCase().endsWith(".class"))
648: return true;
649: return false;
650: }
651: }
652:
653: static final class FolderFileNameFilter implements FileFilter {
654: public boolean accept(final File f) {
655: if (f.isDirectory())
656: return true;
657: return false;
658: }
659: }
660:
661: static final JavaFileNameFilter javaFileNameFilter = new JavaFileNameFilter();
662:
663: // accepts dir and class files
664: public static final ClassFileNameFilter classFileNameFilter = new ClassFileNameFilter();
665: /** Folders only.
666: */
667: public static final FolderFileNameFilter folderFileNameFilter = new FolderFileNameFilter();
668:
669: public static void getAllJavaFilesRecurse(final File root,
670: final List<File> allFiles, final boolean withFolders,
671: final boolean ignoringDirsStartingWithDot) {
672: if (root == null)
673: return;
674: if (!root.exists())
675: return;
676:
677: for (File fi : root.listFiles(javaFileNameFilter)) {
678: if (fi.isDirectory()) {
679: if (ignoringDirsStartingWithDot
680: && fi.getName().startsWith(".")) // ignores settings folders
681: {
682: continue;
683: }
684:
685: if (withFolders) {
686: allFiles.add(fi);
687: }
688:
689: getAllJavaFilesRecurse(fi, allFiles, withFolders,
690: ignoringDirsStartingWithDot);
691: } else {
692: allFiles.add(fi);
693: }
694: }
695: }
696:
697: public static void getAllClassFilesRecurse(final File root,
698: final List<File> allFiles,
699: final boolean ignoringDirsStartingWithDot) {
700: if (root == null)
701: return;
702: if (!root.exists())
703: return;
704:
705: for (File fi : root.listFiles(classFileNameFilter)) {
706: if (fi.isDirectory()) {
707: if (ignoringDirsStartingWithDot
708: && fi.getName().startsWith(".")) // ignores settings folders
709: {
710: continue;
711: }
712: getAllClassFilesRecurse(fi, allFiles,
713: ignoringDirsStartingWithDot);
714: } else {
715: allFiles.add(fi);
716: }
717: }
718: }
719:
720: /** With the root itself.
721: */
722: @tide.annotations.Recurse
723: public static void getFoldersRecurse(final File root,
724: final List<File> allFiles,
725: final boolean ignoringDirsStartingWithDot) {
726: if (root == null)
727: return;
728: if (!root.exists())
729: return;
730: if (root.isFile())
731: return;
732:
733: allFiles.add(root);
734:
735: File[] fis = root.listFiles(folderFileNameFilter);
736: if (fis != null) {
737: for (File fi : fis) {
738: //if(fi.isDirectory())
739: //{
740: if (ignoringDirsStartingWithDot
741: && fi.getName().startsWith(".")) {
742: continue;
743: }
744:
745: // allFiles.add(fi); // [Feb2008]: ERROR: adds twice ! Now we're sure it is added below, cause is !=null, exists an is not file.
746:
747: getFoldersRecurse(fi, allFiles,
748: ignoringDirsStartingWithDot);
749: //}
750: }
751: }
752: /* the case of some system files.
753: else
754: {
755: //System.out.println("Null dir for "+root);
756: }*/
757:
758: }
759:
760: /** Recurse into root, but don't counts directories.
761: */
762: @tide.annotations.Recurse
763: public static int getNumberOfFiles(File root) {
764: int count = 0;
765: for (File f : root.listFiles()) {
766: if (f.isDirectory())
767: count += getNumberOfFiles(f);
768: else if (f.isFile()) {
769: count++;
770: }
771: }
772: return count;
773: }
774:
775: /** @return a "; "separated list or the absolute file paths.
776: Be careful, some apps don't want the space. (just remove it)
777: */
778: public static String filesToList(List<File> files) {
779: StringBuilder sb = new StringBuilder();
780: for (int i = 0; i < files.size(); i++) {
781: sb.append(files.get(i));
782: if (i < files.size() - 1)
783: sb.append("; ");
784: }
785: return sb.toString();
786: }
787:
788: public static List<File> getFilesFromList(String list,
789: boolean onlyExisting) {
790: List<File> files = new ArrayList<File>();
791: for (String pi : list.split("\\;")) {
792: if (pi.trim().length() > 0) {
793: File fi = new File(pi.trim()); // the trim is important !!!! BE CAREFUL !!
794: if (onlyExisting) {
795: if (fi.exists()) {
796: files.add(fi);
797: }
798: } else {
799: files.add(fi);
800: }
801:
802: //System.out.println("File "+pi+" => "+new File(pi));
803:
804: }
805: }
806: return files;
807: }
808:
809: public static List<File> getFilesOnly_notRecurse(File root) {
810: List<File> files = new ArrayList<File>();
811: if (root != null && root.exists()) {
812: for (File f : root.listFiles()) {
813: if (!f.isDirectory())
814: files.add(f);
815: }
816: }
817: return files;
818: }
819:
820: /** remove the ".." in the path (resolve them!)
821: */
822: public static File removeRelativePaths(File f) {
823: File dest = f;
824: try // remove the ..
825: {
826: dest = dest.getCanonicalFile();
827: } catch (Exception e) {
828: Basics.ignore(e);
829: }
830: return dest;
831: }
832:
833: static DecimalFormat format0 = new DecimalFormat("0.0");
834:
835: public static String formatSize(long s) {
836: if (s > 1e9) {
837: double gs = s / 1.0e9;
838: if (gs < 4) // [Nov2007]: changed from 2 to 4
839: {
840: return format0.format(gs) + " GB";
841: } else {
842: return "" + (int) Math.round(gs) + " GB";
843: }
844: }
845: if (s > 1e6) {
846: double ms = s / 1.0e6;
847: if (ms < 4) {
848: return format0.format(ms) + " MB";
849: } else {
850: return "" + (int) Math.round(ms) + " MB";
851: }
852: } else if (s > 1e3) {
853: double ks = s / 1.0e3;
854: if (ks < 4) {
855: return format0.format(ks) + " kB";
856: } else {
857: return "" + (int) Math.round(ks) + " kB";
858: }
859:
860: } else {
861: return s + " B";
862: }
863: }
864:
865: /** Also called suffix, the text after the last dot.
866: * If none, returns the whole name.
867: * Makes sense on linux, where files named path/to/prop are often seen.
868: * So we had a "prop" file.
869: */
870: public static String getExtension(String fileName) {
871: int posPt = fileName.lastIndexOf('.');
872: if (posPt < 0)
873: return fileName;
874: return fileName.substring(posPt + 1);
875: }
876:
877: /** From Gatfer more Puzzlers...
878: */
879: static void skipFully(final InputStream in, long nBytes)
880: throws IOException {
881: long remaining = nBytes;
882: while (remaining != 0) {
883: long skipped = in.skip(remaining);
884: if (skipped == 0)
885: throw new EOFException();
886: remaining -= skipped;
887: }
888: }
889:
890: /* not working now!!
891: public static void main(String[] arguments) throws Exception
892: {
893: File f = new File("c:/temp/a.gv");
894: List ld = Arrays.asList("aa",1,2.3);
895: FileUtils.saveZippedVectorToFile(f,ld);
896: System.out.println(""+ FileUtils.loadZippedVectorFromFile(f));
897: }*/
898:
899: }
|