merge the formfield patch from ooo-build
[ooovba.git] / transex3 / source / filter / utils / FileHelper.java
blob4b40cdfc62560bd19043ac4cade3fd9dd45b172f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: FileHelper.java,v $
10 * $Revision: 1.13 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package com.sun.star.filter.config.tools.utils;
33 // __________ Imports __________
35 import java.io.*;
36 import java.lang.*;
37 import java.net.*;
38 import java.util.*;
40 // __________ Implementation __________
42 /**
43 * It collects some static helper functons to handle file system specific problems.
44 * Sometimes it's neccessary to convert URL from/to system pathes;
45 * or from string notation to structural versions (e.g. com.sun.star.util.URL).
46 * And sometimes java had another notation then the office it has.
47 * Further it provides functionality to work easiear with the java.io.File class of java.
51 public class FileHelper
53 // ____________________
55 /**
56 * Because the office need URLs for loading/saving documents
57 * we must convert used system pathes.
58 * And java use another notation for file URLs ... correct it.
60 * @param aSystemPath
61 * represent the file in system notation
63 * @return [String]
64 * a file url which represent the given system path
66 public static java.lang.String getFileURLFromSystemPath(java.io.File aSystemPath)
68 System.out.println("TODO: must be adapted to java 1.3 :-(");
69 System.exit(-1);
70 /*TODO_JAVA
71 try
73 sFileURL = aSystemPath.toURI().toURL().toString();
75 catch( MalformedURLException exWrong )
77 sFileURL = null;
79 */
80 java.lang.String sFileURL = null;
82 // problem of java: file URL's are coded with 1 slash instead of 2 or 3 ones!
83 // => correct this problem first, otherwise office can't use these URL's
84 if(
85 (sFileURL != null ) &&
86 (sFileURL.startsWith("file:/") == true ) &&
87 (sFileURL.startsWith("file://") == false)
90 java.lang.StringBuffer sWorkBuffer = new java.lang.StringBuffer(sFileURL);
91 sWorkBuffer.insert(6,"//");
92 sFileURL = sWorkBuffer.toString();
95 return sFileURL;
98 // ____________________
101 * The same as getFileURLFromSystemPath() before but uses string parameter instead
102 * of a java.io.File type. It exist to supress converting of neccessary parameters in the
103 * outside code. But of course getFileURLFromSystemPath(File) will be a little bit faster
104 * then this method ...
106 * @param sSystemPath
107 * represent the file in system notation
109 * @return [String]
110 * a file url which represent the given system path
112 public static java.lang.String getFileURLFromSystemPath(java.lang.String sSystemPath)
114 return getFileURLFromSystemPath(new java.io.File(sSystemPath));
117 // ____________________
120 * Does the same as getFileURLFromSystemPath() before ... but uses
121 * the given protocol string (e.g."http://") insted of "file:///".
123 * @param aSystemPath
124 * represent the file in system notation
126 * @param aBasePath
127 * define the base path of the aSystemPath value,
128 * which must be replaced with the value of "sServerPath".
130 * @param sServerURL
131 * Will be used to replace sBasePath.
133 * @example
134 * System Path = "d:\test\file.txt"
135 * Base Path = "d:\test"
136 * Server Path = "http://alaska:8000"
137 * => "http://alaska:8000/file.txt"
139 * @return [String]
140 * an url which represent the given system path
141 * and uses the given protocol
143 public static java.lang.String getURLWithProtocolFromSystemPath(java.io.File aSystemPath,
144 java.io.File aBasePath ,
145 java.lang.String sServerURL )
147 System.out.println("TODO: must be adapted to java 1.3 :-(");
148 System.exit(-1);
150 java.lang.String sFileURL = FileHelper.getFileURLFromSystemPath(aSystemPath);
151 java.lang.String sBaseURL = FileHelper.getFileURLFromSystemPath(aBasePath );
153 // cut last '/'!
154 if (sBaseURL.lastIndexOf('/')==(sBaseURL.length()-1))
155 sBaseURL = sBaseURL.substring(0,sBaseURL.length()-1);
157 // cut last '/'!
158 if (sServerURL.lastIndexOf('/')==(sServerURL.length()-1))
159 sServerURL = sServerURL.substring(0,sServerURL.length()-1);
161 //TODO_JAVA java.lang.String sURL = sFileURL.replaceFirst(sBaseURL,sServerURL);
162 java.lang.String sURL = null;
163 return sURL;
166 // ____________________
169 * The same as getURLWithProtocolFromSystemPath() before but uses string parameter instead
170 * of a java.io.File types. It exist to supress converting of neccessary parameters in the
171 * outside code. But of course getURLWithProtocolFromSystemPath(File,File,String) will be
172 * a little bit faster then this method ...
174 * @param sSystemPath
175 * represent the file in system notation
177 * @param sBasePath
178 * define the base path of the aSystemPath value,
179 * which must be replaced with the value of "sServerPath".
181 * @param sServerPath
182 * Will be used to replace sBasePath.
184 * @example
185 * System Path = "d:\test\file.txt"
186 * Base Path = "d:\test"
187 * Server Path = "http://alaska:8000"
188 * => "http://alaska:8000/file.txt"
190 * @return [String]
191 * an url which represent the given system path
192 * and uses the given protocol
194 public static java.lang.String getURLWithProtocolFromSystemPath(java.lang.String sSystemPath,
195 java.lang.String sBasePath ,
196 java.lang.String sServerPath)
198 return getURLWithProtocolFromSystemPath(new java.io.File(sSystemPath), new java.io.File(sBasePath), sServerPath);
201 //_________________________________
204 * Return a list of all available files of a directory.
205 * We filter sub directories. All other files
206 * are returned. So they can be used for further purposes.
207 * One parameter define the start directory,
208 * another one enable/disable recursive search into sub directories.
210 * @param aRoot
211 * the start directory, which should be analyzed.
213 * @param bRecursive
214 * enable/disable search in sub directories.
216 * @return [Vector]
217 * a filtered list of java java.io.File objects of all available files
218 * of the start dir (and may of its sub directories).
220 public static java.util.Vector getSystemFilesFromDir(java.io.File aRoot ,
221 boolean bRecursive)
223 java.io.File[] lAllFiles = aRoot.listFiles();
224 if (lAllFiles == null)
225 return null;
227 int c = lAllFiles.length;
228 java.util.Vector lFilteredFiles = new java.util.Vector(c);
229 for (int i=0; i<c; ++i)
231 // simple files!
232 if (lAllFiles[i].isFile())
233 lFilteredFiles.add(lAllFiles[i]);
234 else
235 // recursion?
236 if (bRecursive && lAllFiles[i].isDirectory())
238 java.util.Vector lSubFiles = FileHelper.getSystemFilesFromDir(lAllFiles[i],bRecursive);
239 if (lSubFiles != null)
241 java.util.Enumeration aSnapshot = lSubFiles.elements();
242 while (aSnapshot.hasMoreElements())
243 lFilteredFiles.add(aSnapshot.nextElement());
248 return lFilteredFiles;
251 //_________________________________
252 /** it converts the given name (e.g. an internal type name) to
253 * an usable system file name.
255 * Do so some special characters (e.g. "/") must be replaced with other ones.
257 * @param sName
258 * the name, which should be analyzed and converted.
260 * @return A valid system file name, which should be similar to the
261 * given name, but does not contain special characters any longer.
263 public static java.lang.String convertName2FileName(String sName)
265 int i = 0;
266 int nLength = sName.length();
267 char[] lBuffer = sName.toCharArray();
269 java.lang.StringBuffer sNewName = new java.lang.StringBuffer(nLength);
270 for (i=0; i<nLength; ++i)
272 char c = lBuffer[i];
273 if (
274 c>=48 && c<=57 // 0-9
276 c>=97 && c<=122 // a-z
278 c>=65 && c<=90 // A-Z
281 sNewName.append(c);
283 else
285 sNewName.append("_");
289 return sNewName.toString();
292 //___________________________________________
294 /** it removes all child nodes of a file system directory.
296 * @param aDirectory
297 * points to the directory, which should be made empty.
299 * @param bFilesOnly
300 * force deletion of files only. If its set to TRUE,
301 * no subdirectory will be removed.
303 * @throw [java.io.IOException]
304 * if some of the child nodes couldn't be removed.
306 public static void makeDirectoryEmpty(java.io.File aDirectory,
307 boolean bFilesOnly)
308 throws java.io.IOException
310 if (!aDirectory.isDirectory())
311 throw new java.io.FileNotFoundException("\""+aDirectory.toString()+"\" is not a directory.");
313 java.io.File[] lChilds = aDirectory.listFiles();
314 for (int f=0; f<lChilds.length; ++f)
316 if (lChilds[f].isDirectory())
318 FileHelper.makeDirectoryEmpty(lChilds[f], bFilesOnly);
319 if (!bFilesOnly)
321 if (!lChilds[f].delete())
322 throw new java.io.IOException("\""+lChilds[f].toString()+"\" could not be deleted.");
325 else
327 if (!lChilds[f].delete())
328 throw new java.io.IOException("\""+lChilds[f].toString()+"\" could not be deleted.");
333 //___________________________________________
335 /** it try to generate a new file with a unique ID
336 * inside given directory.
338 * Call this method with a directory and a base name for
339 * a file. It will be used to generate a new files inside
340 * the directory. Existing files will be checked and new file
341 * name will be tested till a non existing file name would be found.
343 * @param aBaseDir
344 * must be a system path
345 * e.g.: "c:\temp"
347 * @param sBaseName
348 * must be a system file name without extensions.
349 * e.g.: "myfile_"
351 * @param sExtension
352 * the whished extension.
353 * e.g.: "dat"
355 * @return A valid file object, if an unique file could be created -
356 * Null otherwhise.
357 * e.g.: "c:\temp\myfile_1.dat"
359 public static java.io.File createUniqueFile(java.io.File aBaseDir ,
360 java.lang.String sBaseName ,
361 java.lang.String sExtension)
363 java.io.File aBaseFile = new java.io.File(aBaseDir, sBaseName);
364 java.io.File aFile = null;
365 long nr = 0;
366 while (aFile == null && nr < java.lang.Long.MAX_VALUE)
368 java.lang.String sFileName = aBaseFile.getPath() + java.lang.String.valueOf(nr) + "." + sExtension;
369 aFile = new java.io.File(sFileName);
370 if (aFile.exists())
371 aFile=null;
372 ++nr;
374 return aFile;
377 //___________________________________________
379 /** reads the complete file, using the right encoding,
380 * into the given string buffer.
382 * @param aFile
383 * must point to a system file, which must exist.
384 * e.g.: "c:\temp\test.txt"
385 * "/tmp/test.txt"
387 * @param sEncoding
388 * will be used to encode the string content
389 * inside the file.
390 * e.g.: "UTF8"
392 * @param sBuffer
393 * used to return the file content.
395 * @throw [IOException]
396 * - if the file couldnt be opened
397 * - if the file does not use the right encoding
399 public static void readEncodedBufferFromFile(java.io.File aFile ,
400 java.lang.String sEncoding,
401 java.lang.StringBuffer sBuffer )
402 throws java.io.IOException
404 if (sEncoding.equals("UTF-8Special"))
406 FileHelper.readAndCheckUTF8File(aFile,sBuffer);
407 return;
410 java.io.FileInputStream aByteStream = new java.io.FileInputStream(aFile.getAbsolutePath());
411 java.io.InputStreamReader aEncodedReader = new java.io.InputStreamReader(aByteStream, sEncoding);
412 char[] aEncodedBuffer = new char[4096];
413 int nReadCount = 0;
415 while((nReadCount=aEncodedReader.read(aEncodedBuffer))>0)
416 sBuffer.append(aEncodedBuffer, 0, nReadCount);
418 aEncodedReader.close();
421 //___________________________________________
422 private static void logEncodingData(java.lang.StringBuffer sLog ,
423 int nUTF8 ,
424 int nByteOrg1 ,
425 int nByteOrg2 ,
426 int nByteOrg3 ,
427 int nByteOrg4 ,
428 int nByte1 ,
429 int nByte2 ,
430 int nByte3 ,
431 int nByte4 ,
432 int nEncodingType)
434 sLog.append("["+nEncodingType+"]\t");
435 sLog.append((int)nUTF8+"\t=");
436 sLog.append("\t"+nByteOrg1+"/"+nByte1);
437 sLog.append("\t"+nByteOrg2+"/"+nByte2);
438 sLog.append("\t"+nByteOrg3+"/"+nByte3);
439 sLog.append("\t"+nByteOrg4+"/"+nByte4);
440 sLog.append("\n");
443 //___________________________________________
444 private static char impl_convertBytesToChar(int nByte1, int nByte2, int nByte3, int nByte4)
446 return (char)((nByte1*0x40000)+(nByte2*0x1000)+(nByte3*0x40)+nByte4);
449 //___________________________________________
450 private static int impl_readAndCheckNextByte(byte[] aBuffer ,
451 int nBufPos ,
452 int nBufLength ,
453 int nMinRange ,
454 int nMaxRange )
455 throws java.lang.Exception
457 if (nBufPos>=nBufLength)
458 throw new java.lang.Exception("impl_readAndCheckNextByte()\nEnd of buffer reached.");
460 int nByte = aBuffer[nBufPos] & 0xFF;
461 if (
462 (nByte < nMinRange) ||
463 (nByte > nMaxRange)
466 throw new java.lang.Exception("impl_readAndCheckNextByte()\nByte does not fit the specified range.");
469 return nByte;
472 //___________________________________________
473 public static void readAndCheckUTF8File(java.io.File aFile ,
474 java.lang.StringBuffer sBuffer)
475 throws java.io.IOException
477 java.io.FileInputStream aByteStream = new java.io.FileInputStream(aFile.getAbsolutePath());
478 byte[] aBuffer = new byte[4096];
479 int nReadCount = 0;
480 int nByteOrg_1 = 0;
481 int nByteOrg_2 = 0;
482 int nByteOrg_3 = 0;
483 int nByteOrg_4 = 0;
484 int nByte_1 = 0;
485 int nByte_2 = 0;
486 int nByte_3 = 0;
487 int nByte_4 = 0;
488 char nUTF8 = 0;
489 int i = 0;
490 int nEncodingType = 0;
491 java.lang.StringBuffer sLog = new java.lang.StringBuffer();
496 while((nReadCount=aByteStream.read(aBuffer))>0)
498 i=0;
499 while (i<nReadCount)
501 nByteOrg_1 = 0;
502 nByteOrg_2 = 0;
503 nByteOrg_3 = 0;
504 nByteOrg_4 = 0;
505 nByte_1 = 0;
506 nByte_2 = 0;
507 nByte_3 = 0;
508 nByte_4 = 0;
509 nUTF8 = 0;
510 nEncodingType = 0;
512 nByteOrg_1 = aBuffer[i++] & 0xFF;
513 nByte_1 = nByteOrg_1;
515 Table 3-6. Well-Formed UTF-8 Byte Sequences
517 ============================================================================
518 Nr. Code Points 1st Byte 2nd Byte 3rd Byte 4th Byte
519 ============================================================================
520 01 U+ 0..U+ 7F 00..7F
521 02 U+ 80..U+ 7FF C2..DF 80..BF
522 03 U+ 800..U+ FFF E0 A0..BF 80..BF
523 04 U+ 1000..U+ CFFF E1..EC 80..BF 80..BF
524 05 U+ D000..U+ D7FF ED 80..9F 80..BF
525 06 U+ E000..U+ FFFF EE..EF 80..BF 80..BF
526 07 U+ 10000..U+ 3FFFF F0 90..BF 80..BF 80..BF
527 08 U+ 40000..U+ FFFFF F1..F3 80..BF 80..BF 80..BF
528 09 U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
530 // ------------------------------------------------------------
531 // 01
532 // 1 byte: 0xxxxxxx
533 // ------------------------------------------------------------
534 if (
535 (nByteOrg_1 >= 0x00) &&
536 (nByteOrg_1 <= 0x7F)
539 nEncodingType = 1;
540 nUTF8 = (char)nByte_1;
542 // ------------------------------------------------------------
543 // 02
544 // 1 byte: 110xxxxx
545 // 2 byte: 101xxxxx
546 // ------------------------------------------------------------
547 else
548 if (
549 (nByteOrg_1 >= 0xC2) &&
550 (nByteOrg_1 <= 0xDF)
553 nEncodingType = 2;
554 nByteOrg_2 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0x80, 0xBF);
555 nByte_1 = nByteOrg_1-0xC2;
556 nByte_2 = nByteOrg_2-0x80;
557 nUTF8 = FileHelper.impl_convertBytesToChar(0,0,nByte_1, nByte_2);
559 // ------------------------------------------------------------
560 // 03
561 // 1 byte: 11100000
562 // 2 byte: 101xxxxx
563 // 3 byte: 10xxxxxx
564 // ------------------------------------------------------------
565 else
566 if (nByteOrg_1 == 0xE0)
568 nEncodingType = 3;
569 nByteOrg_2 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0xA0, 0xBF);
570 nByteOrg_3 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0x80, 0xBF);
571 nByte_2 = nByteOrg_2-0xA0;
572 nByte_3 = nByteOrg_3-0x80;
573 nUTF8 = FileHelper.impl_convertBytesToChar(0,0,nByte_2, nByte_3);
575 // ------------------------------------------------------------
576 // 04
577 // 1 byte: 111xxxxx
578 // 2 byte: 10xxxxxx
579 // 3 byte: 10xxxxxx
580 // ------------------------------------------------------------
581 else
582 if (
583 (nByteOrg_1 >= 0xE1) &&
584 (nByteOrg_1 <= 0xEC)
587 nEncodingType = 4;
588 nByteOrg_2 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0x80, 0xBF);
589 nByteOrg_3 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0x80, 0xBF);
590 nByte_1 = nByteOrg_1-0xE1;
591 nByte_2 = nByteOrg_2-0x80;
592 nByte_3 = nByteOrg_3-0x80;
593 nUTF8 = FileHelper.impl_convertBytesToChar(0,nByte_1, nByte_2, nByte_3);
595 // ------------------------------------------------------------
596 // 05
597 // 1 byte: 11101101
598 // 2 byte: 10xxxxxx
599 // 3 byte: 10xxxxxx
600 // ------------------------------------------------------------
601 else
602 if (nByteOrg_1 == 0xED)
604 nEncodingType = 5;
605 nByteOrg_2 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0x80, 0x9F);
606 nByteOrg_3 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0x80, 0xBF);
607 nByte_2 = nByteOrg_2-0x80;
608 nByte_3 = nByteOrg_3-0x80;
609 nUTF8 = FileHelper.impl_convertBytesToChar(0,0, nByte_2, nByte_3);
611 // ------------------------------------------------------------
612 // 06
613 // 1 byte: 1110111x
614 // 2 byte: 10xxxxxx
615 // 3 byte: 10xxxxxx
616 // ------------------------------------------------------------
617 else
618 if (
619 (nByteOrg_1 >= 0xEE) &&
620 (nByteOrg_1 <= 0xEF)
623 nEncodingType = 6;
624 nByteOrg_2 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0x80, 0xBF);
625 nByteOrg_3 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0x80, 0xBF);
626 nByte_1 = nByteOrg_1-0xEE;
627 nByte_2 = nByteOrg_2-0x80;
628 nByte_3 = nByteOrg_3-0x80;
629 nUTF8 = FileHelper.impl_convertBytesToChar(0,nByte_1, nByte_2, nByte_3);
631 // ------------------------------------------------------------
632 // 07
633 // 1 byte: 11110000
634 // 2 byte: 1001xxxx
635 // 3 byte: 10xxxxxx
636 // 4 byte: 10xxxxxx
637 // ------------------------------------------------------------
638 else
639 if (nByteOrg_1 == 0xF0)
641 nEncodingType = 7;
642 nByteOrg_2 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0x90, 0xBF);
643 nByteOrg_3 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0x80, 0xBF);
644 nByteOrg_4 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0x80, 0xBF);
645 nByte_2 = nByteOrg_2-0x90;
646 nByte_3 = nByteOrg_3-0x80;
647 nByte_4 = nByteOrg_4-0x80;
648 nUTF8 = FileHelper.impl_convertBytesToChar(0, nByte_2, nByte_3, nByte_4);
650 // ------------------------------------------------------------
651 // 08
652 // 1 byte: 111100xx
653 // 2 byte: 10xxxxxx
654 // 3 byte: 10xxxxxx
655 // 3 byte: 10xxxxxx
656 // ------------------------------------------------------------
657 else
658 if (
659 (nByteOrg_1 >= 0xF1) &&
660 (nByteOrg_1 <= 0xF3)
663 nEncodingType = 8;
664 nByteOrg_2 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0x80, 0xBF);
665 nByteOrg_3 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0x80, 0xBF);
666 nByteOrg_4 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0x80, 0xBF);
667 nByte_1 = nByteOrg_1-0xF1;
668 nByte_2 = nByteOrg_2-0x80;
669 nByte_3 = nByteOrg_3-0x80;
670 nByte_4 = nByteOrg_4-0x80;
671 nUTF8 = FileHelper.impl_convertBytesToChar(nByte_1, nByte_2, nByte_3, nByte_4);
673 // ------------------------------------------------------------
674 // 09
675 // 1 byte: 11110100
676 // 2 byte: 10xxxxxx
677 // 3 byte: 10xxxxxx
678 // 4 byte: 10xxxxxx
679 // ------------------------------------------------------------
680 else
681 if (nByteOrg_1 == 0xF0)
683 nEncodingType = 9;
684 nByteOrg_2 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0x80, 0xBF);
685 nByteOrg_3 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0x80, 0xBF);
686 nByteOrg_4 = FileHelper.impl_readAndCheckNextByte(aBuffer, i++, nReadCount, 0x80, 0xBF);
687 nByte_2 = nByteOrg_2-0x80;
688 nByte_3 = nByteOrg_3-0x80;
689 nByte_4 = nByteOrg_4-0x80;
690 nUTF8 = FileHelper.impl_convertBytesToChar(0, nByte_2, nByte_3, nByte_4);
692 // wrong encoding ?
693 else
695 throw new java.lang.Exception("Non well formed UTF-8 encoding.");
698 sBuffer.append(nUTF8);
699 // -> DEBUG !
700 FileHelper.logEncodingData(sLog, nUTF8, nByteOrg_1, nByteOrg_2, nByteOrg_3, nByteOrg_4, nByte_1, nByte_2, nByte_3, nByte_4, nEncodingType);
701 // <- DEBUG !
706 catch(java.lang.Throwable ex)
708 // -> DEBUG !
709 FileHelper.logEncodingData(sLog, nUTF8, nByteOrg_1, nByteOrg_2, nByteOrg_3, nByteOrg_4, nByte_1, nByte_2, nByte_3, nByte_4, nEncodingType);
711 java.io.File aDir = new java.io.File(aFile.getParent());
712 java.lang.String sDump = aFile.getName();
713 java.io.File aDump = FileHelper.createUniqueFile(aDir, sDump, "dump");
714 FileHelper.writeEncodedBufferToFile(aDump, "UTF-8", false, sLog);
715 // <- DEBUG !
717 java.lang.String sMsg = "File '"+aFile.getPath()+"' is not encoded right as UTF-8.";
718 throw new java.io.IOException(sMsg);
721 aByteStream.close();
724 //___________________________________________
726 /** writes the given string buffer into the specified file
727 * using the specified encoding.
729 * Further it can be set, if the file should be expanded
730 * or replaced by this new string buffer.
732 * @param aFile
733 * must point to a system file. It can already exist!
734 * e.g.: "c:\temp\test.txt"
735 * "/tmp/test.txt"
737 * @param sEncoding
738 * will be used to encode the string content inside the file.
739 * e.g.: "UTF8"
741 * @param bAppend
742 * specify if an already existing file will be
743 * expanded or replaced.
745 * @param sBuffer
746 * the new string content for this file.
748 public static void writeEncodedBufferToFile(java.io.File aFile ,
749 java.lang.String sEncoding,
750 boolean bAppend ,
751 java.lang.StringBuffer sBuffer )
752 throws java.io.IOException
754 java.io.FileOutputStream aByteStream = new java.io.FileOutputStream(aFile.getAbsolutePath(), bAppend);
755 java.io.OutputStreamWriter aEncodedWriter = new java.io.OutputStreamWriter(aByteStream, sEncoding);
757 java.lang.String sTemp = sBuffer.toString();
758 aEncodedWriter.write(sTemp, 0, sTemp.length());
760 aEncodedWriter.flush();
761 aEncodedWriter.close();
763 if (!aFile.exists())
764 throw new java.io.IOException("File \""+aFile.getAbsolutePath()+"\" not written correctly.");