Source Code Cross Referenced for MiscHelper.java in  » Workflow-Engines » JFolder » org » jfolder » common » utils » misc » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Workflow Engines » JFolder » org.jfolder.common.utils.misc 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * JFolder, Copyright 2001-2006 Gary Steinmetz
0003:         *
0004:         * Distributable under LGPL license.
0005:         * See terms of license at gnu.org.
0006:         */
0007:
0008:        package org.jfolder.common.utils.misc;
0009:
0010:        //base classes
0011:        import java.io.ByteArrayInputStream;
0012:        import java.io.ByteArrayOutputStream;
0013:        import java.io.File;
0014:        import java.io.FileInputStream;
0015:        import java.io.FileOutputStream;
0016:        import java.io.FileReader;
0017:        import java.io.FileWriter;
0018:        import java.io.IOException;
0019:        import java.io.InputStream;
0020:        import java.io.InputStreamReader;
0021:        import java.io.OutputStream;
0022:        import java.io.OutputStreamWriter;
0023:        import java.io.PrintWriter;
0024:        import java.io.Reader;
0025:        import java.io.StringWriter;
0026:        import java.math.BigDecimal;
0027:        import java.math.BigInteger;
0028:        import java.sql.Timestamp;
0029:        import java.text.ParseException;
0030:        import java.text.SimpleDateFormat;
0031:        import java.util.ArrayList;
0032:        import java.util.Calendar;
0033:        import java.util.Date;
0034:        import java.util.GregorianCalendar;
0035:        import java.util.StringTokenizer;
0036:        import java.util.regex.Matcher;
0037:        import java.util.regex.Pattern;
0038:        import java.util.zip.ZipEntry;
0039:        import java.util.zip.ZipInputStream;
0040:        import java.util.zip.ZipOutputStream;
0041:
0042:        //project specific classes
0043:        import org.jfolder.common.UnexpectedSystemException;
0044:
0045:        //other classes
0046:
0047:        public final class MiscHelper {
0048:
0049:            private MiscHelper() {
0050:            }
0051:
0052:            public final static void println() {
0053:                localPrintln();
0054:            }
0055:
0056:            public final static void println(Object inMessage) {
0057:                localPrintln(inMessage);
0058:            }
0059:
0060:            public final static void print(Object inMessage) {
0061:                localPrint(inMessage);
0062:            }
0063:
0064:            public final static void writeMessage(Object inO, String inMessage) {
0065:                writeMessage(inO.getClass(), inMessage);
0066:            }
0067:
0068:            public final static void writeMessage(Class inC, String inMessage) {
0069:                String source = inC.getName();
0070:                String startBorder = ("<!-- " + source + " -->");
0071:                String endBorder = ("<!-- "
0072:                        + fixString("", source.length(), '-') + " -->");
0073:                MiscHelper.println(startBorder);
0074:                MiscHelper.println(inMessage);
0075:                MiscHelper.println(endBorder);
0076:            }
0077:
0078:            public final static void writeMessage(Object inO, Throwable inE) {
0079:                writeMessage(inO.getClass(), inE);
0080:            }
0081:
0082:            public final static void writeMessage(Class inC, Throwable inE) {
0083:                String source = inC.getName();
0084:                String startBorder = ("<!-- " + source + " -->");
0085:                String endBorder = ("<!-- "
0086:                        + fixString("", source.length(), '-') + " -->");
0087:                MiscHelper.println(startBorder);
0088:                MiscHelper.println(inE.getMessage());
0089:                localPrintStackTrace(inE);
0090:                MiscHelper.println(endBorder);
0091:            }
0092:
0093:            //////////////////////////////////////////////////////////////////
0094:            private final static void localPrintln() {
0095:                //System.out.println();
0096:            }
0097:
0098:            private final static void localPrintln(Object inMessage) {
0099:                //System.out.println(inMessage);
0100:            }
0101:
0102:            private final static void localPrint(Object inMessage) {
0103:                //System.out.print(inMessage);
0104:            }
0105:
0106:            private final static void localPrintStackTrace(Throwable inE) {
0107:                inE.printStackTrace();
0108:            }
0109:
0110:            //
0111:            public final static int getTotalMemory() {
0112:
0113:                int outValue = 0;
0114:
0115:                //
0116:                Runtime rt = Runtime.getRuntime();
0117:                outValue += rt.maxMemory();
0118:                //outValue -= rt.totalMemory();
0119:                //outValue += rt.freeMemory();
0120:
0121:                return outValue;
0122:            }
0123:
0124:            public final static int getTotalFreeMemory() {
0125:
0126:                int outValue = 0;
0127:
0128:                //
0129:                Runtime rt = Runtime.getRuntime();
0130:                outValue += rt.maxMemory();
0131:                outValue -= rt.totalMemory();
0132:                outValue += rt.freeMemory();
0133:
0134:                return outValue;
0135:            }
0136:
0137:            public final static void profileMemory() {
0138:                Runtime rt = Runtime.getRuntime();
0139:                MiscHelper.writeMessage(MiscHelper.class, "Total Memory = "
0140:                        + rt.totalMemory());
0141:                MiscHelper.writeMessage(MiscHelper.class, "Maximum Memory = "
0142:                        + rt.maxMemory());
0143:                MiscHelper.writeMessage(MiscHelper.class, "Free Memory = "
0144:                        + rt.freeMemory());
0145:            }
0146:
0147:            public final static String fixString(String inSource, int inLength) {
0148:                return fixString(inSource, inLength, ' ');
0149:            }
0150:
0151:            public final static String fixString(String inSource, int inLength,
0152:                    char inPad) {
0153:
0154:                String outValue = null;
0155:
0156:                StringBuffer sb = new StringBuffer(shortenString(inSource,
0157:                        inLength));
0158:
0159:                while (sb.length() < inLength) {
0160:                    sb.append(inPad);
0161:                }
0162:
0163:                outValue = sb.toString();
0164:
0165:                return outValue;
0166:            }
0167:
0168:            public final static String shortenString(String inSource) {
0169:                return shortenString(inSource, 20);
0170:            }
0171:
0172:            public final static String shortenString(String inSource,
0173:                    int inLength) {
0174:
0175:                String outValue = null;
0176:
0177:                if (inSource.length() <= inLength) {
0178:                    outValue = inSource;
0179:                } else {
0180:
0181:                    int fadLength = Math.min(3, inLength);
0182:
0183:                    outValue = inSource.substring(0, inLength - fadLength);
0184:                    for (int i = 0; i < fadLength; i++) {
0185:                        outValue = outValue + ".";
0186:                    }
0187:                }
0188:
0189:                return outValue;
0190:            }
0191:
0192:            public final static boolean isRegularExpressionMatch(
0193:                    String inInput, String inPattern, boolean inMultiline) {
0194:
0195:                boolean outValue = false;
0196:
0197:                Pattern pattern = null;
0198:                if (inMultiline) {
0199:                    pattern = Pattern.compile(inPattern, Pattern.MULTILINE);
0200:                } else {
0201:                    pattern = Pattern.compile(inPattern);
0202:                }
0203:                Matcher matcher = pattern.matcher(inInput);
0204:                outValue = matcher.matches();
0205:
0206:                return outValue;
0207:            }
0208:
0209:            public final static Object newInstance(Class inClass) {
0210:                try {
0211:
0212:                    Object outValue = null;
0213:
0214:                    outValue = inClass.newInstance();
0215:
0216:                    return outValue;
0217:                } catch (InstantiationException ie) {
0218:                    throw new UnexpectedSystemException(ie);
0219:                } catch (IllegalAccessException iae) {
0220:                    throw new UnexpectedSystemException(iae);
0221:                }
0222:            }
0223:
0224:            public final static Class getClassByName(String inName) {
0225:                return getClassByName(inName, true);
0226:            }
0227:
0228:            public final static Class getClassByName(String inName,
0229:                    boolean inThrow) {
0230:                //if (name != null) {
0231:                Class outValue = null;
0232:                if (inName.equals(Boolean.TYPE.getName())) {
0233:                    outValue = Boolean.TYPE;
0234:                } else if (inName.equals(Byte.TYPE.getName())) {
0235:                    outValue = Byte.TYPE;
0236:                } else if (inName.equals(Short.TYPE.getName())) {
0237:                    outValue = Short.TYPE;
0238:                } else if (inName.equals(Character.TYPE.getName())) {
0239:                    outValue = Character.TYPE;
0240:                } else if (inName.equals(Integer.TYPE.getName())) {
0241:                    outValue = Integer.TYPE;
0242:                } else if (inName.equals(Long.TYPE.getName())) {
0243:                    outValue = Long.TYPE;
0244:                } else if (inName.equals(Float.TYPE.getName())) {
0245:                    outValue = Float.TYPE;
0246:                } else if (inName.equals(Double.TYPE.getName())) {
0247:                    outValue = Double.TYPE;
0248:                } else if (inName.equals(Void.TYPE.getName())) {
0249:                    outValue = Void.TYPE;
0250:                } else {
0251:                    try {
0252:                        outValue = Class.forName(inName);
0253:                    } catch (ClassNotFoundException cnfe) {
0254:                        if (inThrow) {
0255:                            throw new UnexpectedSystemException(cnfe);
0256:                        } else {
0257:                            outValue = null;
0258:                        }
0259:                    }
0260:                }
0261:
0262:                return outValue;
0263:            }
0264:
0265:            public final static String fromSerializedDataToCData(byte inBa[]) {
0266:
0267:                StringBuffer outValue = new StringBuffer();
0268:
0269:                for (int i = 0; i < inBa.length; i++) {
0270:                    int baseDec = inBa[i] & 0x00ff;
0271:                    String baseHex = Integer.toHexString(baseDec);
0272:                    //TO DO: check for exception
0273:                    if (baseHex.length() == 1) {
0274:                        outValue.append("0");
0275:                        outValue.append(baseHex);
0276:                        //MiscHelper.println(baseDec + " becomes - 0" + baseHex);
0277:                    } else {
0278:                        //MiscHelper.println(baseDec + " becomes - " + baseHex);
0279:                        outValue.append(baseHex);
0280:                    }
0281:                }
0282:
0283:                return outValue.toString();
0284:            }
0285:
0286:            public final static byte[] fromCDataToSerializedData(String inCd) {
0287:
0288:                byte outValue[] = new byte[inCd.length() / 2];
0289:
0290:                if ((inCd.length() % 2) != 0) {
0291:                    throw new UnexpectedSystemException(inCd
0292:                            + " does not have an even length");
0293:                }
0294:
0295:                for (int i = 0; i < outValue.length; i++) {
0296:                    int offset = i * 2;
0297:                    String hexNum = inCd.substring(offset, offset + 2);
0298:                    byte nextByte = (byte) (Integer.parseInt(hexNum, 16) & 0x00ff);
0299:                    outValue[i] = nextByte;
0300:                }
0301:
0302:                return outValue;
0303:            }
0304:
0305:            public final static boolean isClassNumber(Class inClass) {
0306:
0307:                boolean outValue = false;
0308:
0309:                String cName = inClass.getName();
0310:                if (cName.equals(Byte.class.getName())
0311:                        || cName.equals(Character.class.getName())
0312:                        || cName.equals(Short.class.getName())
0313:                        || cName.equals(Integer.class.getName())
0314:                        || cName.equals(Long.class.getName())
0315:                        || cName.equals(Float.class.getName())
0316:                        || cName.equals(Double.class.getName())
0317:                        || cName.equals(Byte.TYPE.getName())
0318:                        || cName.equals(Character.TYPE.getName())
0319:                        || cName.equals(Short.TYPE.getName())
0320:                        || cName.equals(Integer.TYPE.getName())
0321:                        || cName.equals(Long.TYPE.getName())
0322:                        || cName.equals(Float.TYPE.getName())
0323:                        || cName.equals(Double.TYPE.getName())
0324:                        || cName.equals(BigInteger.class.getName())
0325:                        || cName.equals(BigDecimal.class.getName())) {
0326:
0327:                    outValue = true;
0328:                }
0329:
0330:                return outValue;
0331:            }
0332:
0333:            public final static BigDecimal fromNumberToBigDecimal(
0334:                    Object inNumber) {
0335:
0336:                BigDecimal outValue = null;
0337:
0338:                String cName = inNumber.getClass().getName();
0339:                if (cName.equals(Byte.class.getName())
0340:                        || cName.equals(Short.class.getName())
0341:                        || cName.equals(Integer.class.getName())
0342:                        || cName.equals(Long.class.getName())
0343:                        || cName.equals(Float.class.getName())
0344:                        || cName.equals(Double.class.getName())
0345:                        || cName.equals(Byte.TYPE.getName())
0346:                        || cName.equals(Short.TYPE.getName())
0347:                        || cName.equals(Integer.TYPE.getName())
0348:                        || cName.equals(Long.TYPE.getName())
0349:                        || cName.equals(Float.TYPE.getName())
0350:                        || cName.equals(Double.TYPE.getName())) {
0351:                    //TO DO: is there any persicion loss?
0352:                    outValue = new BigDecimal(((Number) inNumber).doubleValue());
0353:                } else if (cName.equals(Character.class.getName())
0354:                        || cName.equals(Character.TYPE.getName())) {
0355:                    outValue = new BigDecimal(((Character) inNumber)
0356:                            .charValue());
0357:                } else if (cName.equals(BigInteger.class.getName())) {
0358:                    outValue = new BigDecimal((BigInteger) inNumber);
0359:                } else if (cName.equals(BigDecimal.class.getName())) {
0360:                    outValue = (BigDecimal) inNumber;
0361:                } else {
0362:                    //registerException("Arithmetic input must be"
0363:                    //  + " one of the following classes - "
0364:                    //  + "java.lang.Byte, java.lang.Character, java.lang.Short, "
0365:                    //  + "java.lang.Integer, java.lang.Long, java.lang.Float, "
0366:                    //  + "java.lang.Double, java.math.BigInteger,
0367:                    // java.lang.BigDecimal");
0368:                    //return null;//TO DO: verify this and all other returns
0369:                }
0370:
0371:                return outValue;
0372:            }
0373:
0374:            public final static Class[] getDecimalClasses() {
0375:                Class outValue[] = new Class[16];
0376:                outValue[0] = Byte.class;
0377:                outValue[1] = Short.class;
0378:                outValue[2] = Integer.class;
0379:                outValue[3] = Long.class;
0380:                outValue[4] = Float.class;
0381:                outValue[5] = Double.class;
0382:                outValue[6] = Byte.TYPE;
0383:                outValue[7] = Short.TYPE;
0384:                outValue[8] = Integer.TYPE;
0385:                outValue[9] = Long.TYPE;
0386:                outValue[10] = Float.TYPE;
0387:                outValue[11] = Double.TYPE;
0388:                outValue[12] = Character.class;
0389:                outValue[13] = Character.TYPE;
0390:                outValue[14] = BigInteger.class;
0391:                outValue[15] = BigDecimal.class;
0392:                return outValue;
0393:            }
0394:
0395:            public final static boolean isStringNonNegativeNumber(String inValue) {
0396:                //
0397:                boolean outValue = true;
0398:
0399:                if (inValue != null) {
0400:                    for (int i = 0; i < inValue.length(); i++) {
0401:                        char nextChar = inValue.charAt(i);
0402:                        if (nextChar == '0' || nextChar == '1'
0403:                                || nextChar == '2' || nextChar == '3'
0404:                                || nextChar == '4' || nextChar == '5'
0405:                                || nextChar == '6' || nextChar == '7'
0406:                                || nextChar == '8' || nextChar == '9') {
0407:                            //do nothing!
0408:                        } else {
0409:                            outValue = false;
0410:                        }
0411:                    }
0412:                    if (inValue.length() == 0) {
0413:                        outValue = false;
0414:                    }
0415:                } else {
0416:                    outValue = false;
0417:                }
0418:
0419:                return outValue;
0420:            }
0421:
0422:            public final static String[] fromClassArrayToStringArray(
0423:                    Class inClasses[]) {
0424:
0425:                String outValue[] = null;
0426:
0427:                if (inClasses != null) {
0428:                    outValue = new String[inClasses.length];
0429:                    for (int i = 0; i < inClasses.length; i++) {
0430:                        outValue[i] = (inClasses[i]).getName();
0431:                    }
0432:                }
0433:
0434:                return outValue;
0435:            }
0436:
0437:            public final static Class[] fromStringArrayToClassArray(
0438:                    String inStrings[]) {
0439:
0440:                Class outValue[] = null;
0441:
0442:                if (inStrings != null) {
0443:                    outValue = new Class[inStrings.length];
0444:                    for (int i = 0; i < inStrings.length; i++) {
0445:                        try {
0446:                            outValue[i] = Class.forName(inStrings[i]);
0447:                        } catch (ClassNotFoundException cnfe) {
0448:                            outValue[i] = null;
0449:                        }
0450:                    }
0451:                }
0452:
0453:                return outValue;
0454:            }
0455:
0456:            public final static String formatTime(long inMillis) {
0457:
0458:                String outValue = "";
0459:
0460:                Date modifiedDate = new Date(inMillis);
0461:                GregorianCalendar gc = new GregorianCalendar();
0462:                gc.setTime(modifiedDate);
0463:                int year = gc.get(Calendar.YEAR);
0464:                int month = gc.get(Calendar.MONTH);
0465:                int day = gc.get(Calendar.DAY_OF_MONTH);
0466:                int hour = gc.get(Calendar.HOUR);
0467:                int minute = gc.get(Calendar.MINUTE);
0468:                int second = gc.get(Calendar.SECOND);
0469:                int amPm = gc.get(Calendar.AM_PM);
0470:
0471:                if (hour == 0) {
0472:                    hour = 12;
0473:                }
0474:
0475:                if (month == Calendar.JANUARY) {
0476:                    outValue = outValue + "Jan ";
0477:                } else if (month == Calendar.FEBRUARY) {
0478:                    outValue = outValue + "Feb ";
0479:                } else if (month == Calendar.MARCH) {
0480:                    outValue = outValue + "Mar ";
0481:                } else if (month == Calendar.APRIL) {
0482:                    outValue = outValue + "Apr ";
0483:                } else if (month == Calendar.MAY) {
0484:                    outValue = outValue + "May ";
0485:                } else if (month == Calendar.JUNE) {
0486:                    outValue = outValue + "Jun ";
0487:                } else if (month == Calendar.JULY) {
0488:                    outValue = outValue + "Jul ";
0489:                } else if (month == Calendar.AUGUST) {
0490:                    outValue = outValue + "Aug ";
0491:                } else if (month == Calendar.SEPTEMBER) {
0492:                    outValue = outValue + "Sep ";
0493:                } else if (month == Calendar.OCTOBER) {
0494:                    outValue = outValue + "Oct ";
0495:                } else if (month == Calendar.NOVEMBER) {
0496:                    outValue = outValue + "Nov ";
0497:                } else if (month == Calendar.DECEMBER) {
0498:                    outValue = outValue + "Dec ";
0499:                }
0500:
0501:                if (day < 10) {
0502:                    outValue = outValue + " ";
0503:                }
0504:
0505:                outValue = outValue + day + ", " + year;
0506:
0507:                if (hour < 10) {
0508:                    outValue = outValue + " ";
0509:                }
0510:
0511:                if (minute < 10) {
0512:                    outValue = outValue + " " + hour + ":0" + minute;
0513:                } else {
0514:                    outValue = outValue + " " + hour + ":" + minute;
0515:                }
0516:
0517:                if (amPm == Calendar.AM) {
0518:                    outValue = outValue + " AM";
0519:                } else if (amPm == Calendar.PM) {
0520:                    outValue = outValue + " PM";
0521:                }
0522:
0523:                return outValue;
0524:            }
0525:
0526:            private final static String representStringAsNumberCode(
0527:                    String inString) {
0528:
0529:                StringBuffer outValue = new StringBuffer();
0530:
0531:                for (int i = 0; i < inString.length(); i++) {
0532:                    char nextChar = inString.charAt(i);
0533:                    int divisor = 10000;
0534:                    while (divisor != 0) {
0535:                        outValue.append((int) (nextChar / divisor));
0536:                        nextChar = (char) (nextChar % divisor);
0537:                        divisor = divisor / 10;
0538:                    }
0539:                }
0540:
0541:                return outValue.toString();
0542:            }
0543:
0544:            private final static String representNumberCodeAsString(
0545:                    String inCode) {
0546:
0547:                StringBuffer outValue = new StringBuffer();
0548:
0549:                for (int i = 0; i < inCode.length(); i = i + 5) {
0550:                    String nextCodeString = inCode.substring(i, i + 5);
0551:                    int nextCode = Integer.parseInt(nextCodeString);
0552:                    outValue.append((char) nextCode);
0553:                }
0554:
0555:                return outValue.toString();
0556:            }
0557:
0558:            public final static void deleteFileOrDirectory(File inFile) {
0559:
0560:                if (inFile.isFile()) {
0561:                    boolean result = inFile.delete();
0562:                    //MiscHelper.println(
0563:                    //    "MiscHelper " + inFile + " delete = " + result);
0564:                    //MiscHelper.println("exists = " + inFile.exists());
0565:                } else {
0566:                    File subFiles[] = inFile.listFiles();
0567:                    for (int i = 0; i < subFiles.length; i++) {
0568:                        deleteFileOrDirectory(subFiles[i]);
0569:                    }
0570:                    boolean result = inFile.delete();
0571:                    //MiscHelper.println(
0572:                    //    "MiscHelper " + inFile + " delete = " + result);
0573:                    //MiscHelper.println("exists = " + inFile.exists());
0574:                }
0575:            }
0576:
0577:            public final static String literalReplace(String inSource,
0578:                    String inMatch, String inReplace) {
0579:
0580:                StringBuffer outValue = new StringBuffer();
0581:
0582:                int startPoint = 0;
0583:                int endPoint = 0;
0584:                int result = 0;
0585:
0586:                while (endPoint != -1) {
0587:                    endPoint = inSource.indexOf(inMatch, startPoint);
0588:                    if (endPoint != -1) {
0589:                        outValue.append(inSource
0590:                                .substring(startPoint, endPoint));
0591:                        outValue.append(inReplace);
0592:                        startPoint = endPoint + inMatch.length();
0593:                    } else {
0594:                        outValue.append(inSource.substring(startPoint));
0595:                    }
0596:                }
0597:
0598:                return outValue.toString();
0599:
0600:            }
0601:
0602:            public final static String readTextFile(File inFile) {
0603:                try {
0604:                    StringBuffer outValue = new StringBuffer();
0605:
0606:                    if (inFile.exists()) {
0607:                        FileReader fr = new FileReader(inFile);
0608:                        int nextChar = 0;
0609:                        while ((nextChar = fr.read()) != -1) {
0610:                            outValue.append((char) nextChar);
0611:                        }
0612:                        fr.close();
0613:                    }
0614:
0615:                    return outValue.toString();
0616:                } catch (IOException ioe) {
0617:                    throw new UnexpectedSystemException(ioe);
0618:                }
0619:            }
0620:
0621:            public final static String readReader(Reader inReader) {
0622:                try {
0623:                    StringBuffer outValue = new StringBuffer();
0624:
0625:                    int nextChar = 0;
0626:                    while ((nextChar = inReader.read()) != -1) {
0627:                        outValue.append((char) nextChar);
0628:                    }
0629:                    inReader.close();
0630:
0631:                    return outValue.toString();
0632:                } catch (IOException ioe) {
0633:                    throw new UnexpectedSystemException(ioe);
0634:                }
0635:            }
0636:
0637:            public final static void writeTextFile(File inFile, String inContent) {
0638:                try {
0639:                    FileWriter fw = new FileWriter(inFile);
0640:                    fw.write(inContent);
0641:                    fw.flush();
0642:                    fw.close();
0643:                } catch (IOException ioe) {
0644:                    throw new UnexpectedSystemException(ioe);
0645:                }
0646:            }
0647:
0648:            public final static void writeBinaryFile(File inFile,
0649:                    byte inContent[]) {
0650:                try {
0651:                    FileOutputStream fos = new FileOutputStream(inFile);
0652:                    fos.write(inContent);
0653:                    fos.flush();
0654:                    fos.close();
0655:                } catch (IOException ioe) {
0656:                    throw new UnexpectedSystemException(ioe);
0657:                }
0658:            }
0659:
0660:            public final static byte[] readBinaryFile(File inFile) {
0661:                try {
0662:                    byte outValue[] = null;
0663:                    //ByteArrayOutputStream outValue = new ByteArrayOutputStream();
0664:
0665:                    //byte buffer[] = new byte[16*1024];
0666:
0667:                    FileInputStream fis = new FileInputStream(inFile);
0668:
0669:                    //int size = 0;
0670:                    //while ((size = fis.read(buffer)) != -1) {
0671:                    //    outValue.write(buffer, 0, size);
0672:                    //}
0673:
0674:                    //return outValue.toByteArray();
0675:
0676:                    outValue = fromInputStreamToBytesWithoutClose(fis);
0677:                    fis.close();
0678:
0679:                    return outValue;
0680:                } catch (IOException ioe) {
0681:                    throw new UnexpectedSystemException(ioe);
0682:                }
0683:            }
0684:
0685:            public final static byte[] fromInputStreamToBytesWithoutClose(
0686:                    InputStream inIs) {
0687:                //
0688:                try {
0689:                    ByteArrayOutputStream outValue = new ByteArrayOutputStream();
0690:
0691:                    byte buffer[] = new byte[16 * 1024];
0692:
0693:                    //FileInputStream fis = new FileInputStream(inFile);
0694:
0695:                    int size = 0;
0696:                    while ((size = inIs.read(buffer)) != -1) {
0697:                        outValue.write(buffer, 0, size);
0698:                    }
0699:
0700:                    //fis.close();
0701:
0702:                    return outValue.toByteArray();
0703:                } catch (IOException ioe) {
0704:                    throw new UnexpectedSystemException(ioe);
0705:                }
0706:            }
0707:
0708:            public final static String removeFileExtension(String inFileName) {
0709:                String outValue = null;
0710:                int periodPoint = inFileName.indexOf('.');
0711:                outValue = inFileName.substring(0, periodPoint);
0712:                return outValue;
0713:            }
0714:
0715:            //ZIP FUNCTIONS
0716:            public final static void zipDirectory(File inDirName,
0717:                    OutputStream inOs) throws IOException {
0718:
0719:                ZipOutputStream zos = new ZipOutputStream(inOs);
0720:                zipDirectory(inDirName, inDirName, zos);
0721:                zos.flush();
0722:                zos.close();
0723:            }
0724:
0725:            private final static void zipDirectory(File inBaseDir,
0726:                    File inCurrentDir, ZipOutputStream inZos)
0727:                    throws IOException {
0728:
0729:                File dirContents[] = inCurrentDir.listFiles();
0730:
0731:                byte buffer[] = new byte[1024];
0732:
0733:                for (int i = 0; i < dirContents.length; i++) {
0734:
0735:                    File nextFile = dirContents[i];
0736:
0737:                    if (nextFile.isFile()) {
0738:
0739:                        FileInputStream fis = new FileInputStream(nextFile);
0740:
0741:                        String entryName = nextFile
0742:                                .getAbsolutePath()
0743:                                .substring(
0744:                                        inBaseDir.getAbsolutePath().length() + 1);
0745:
0746:                        entryName = entryName.replace(File.separatorChar, '/');
0747:
0748:                        inZos.putNextEntry(new ZipEntry(entryName));
0749:
0750:                        int len = 0;
0751:
0752:                        while ((len = fis.read(buffer, 0, buffer.length)) != -1) {
0753:                            inZos.write(buffer, 0, len);
0754:                        }
0755:
0756:                        inZos.closeEntry();
0757:                        fis.close();
0758:                    } else {
0759:                        zipDirectory(inBaseDir, nextFile, inZos);
0760:                    }
0761:                }
0762:            }
0763:
0764:            private final static void createDir(File inDir) {
0765:                if (!inDir.exists()) {
0766:                    createDir(inDir.getParentFile());
0767:                    inDir.mkdir();
0768:                }
0769:            }
0770:
0771:            public final static void parseZipInfo(InputStream inIs,
0772:                    File inBaseDir) throws IOException {
0773:
0774:                ZipInputStream zis = new ZipInputStream(inIs);
0775:
0776:                ZipEntry nextEntry = null;
0777:
0778:                while ((nextEntry = zis.getNextEntry()) != null) {
0779:                    String nextEntryName = nextEntry.getName();
0780:                    nextEntryName = nextEntryName.replace('/',
0781:                            File.separatorChar);
0782:                    File nextEntryFile = new File(inBaseDir, nextEntryName);
0783:                    if (nextEntry.isDirectory()) {
0784:                        createDir(nextEntryFile);
0785:                    } else {
0786:                        createDir(nextEntryFile.getParentFile());
0787:                        FileOutputStream fos = new FileOutputStream(
0788:                                nextEntryFile);
0789:
0790:                        byte buffer[] = new byte[1024];
0791:                        int len = 0;
0792:                        while ((len = zis.read(buffer, 0, buffer.length)) != -1) {
0793:                            fos.write(buffer, 0, len);
0794:                        }
0795:
0796:                        fos.flush();
0797:                        fos.close();
0798:                    }
0799:                    //MiscHelper.println(nextEntry.getName());
0800:                    zis.closeEntry();
0801:                }
0802:
0803:                zis.close();
0804:            }
0805:
0806:            public final static String prependSpaces(int inSpaceCount[]) {
0807:
0808:                final String TWO_SPACES = "&#160;&#160;";
0809:
0810:                StringBuffer outValue = new StringBuffer();
0811:
0812:                for (int i = 0; i < inSpaceCount[0]; i = i + 2) {
0813:                    outValue.append(TWO_SPACES);
0814:                }
0815:
0816:                return outValue.toString();
0817:            }
0818:
0819:            public final static long currentUTC() {
0820:
0821:                long outValue = 0;
0822:
0823:                outValue = fromLocalizedTimeToUTCTime(System
0824:                        .currentTimeMillis());
0825:
0826:                return outValue;
0827:            }
0828:
0829:            public final static long fromUTCTimeToLocalizedTime(long inTime) {
0830:
0831:                long outValue = 0;
0832:
0833:                Calendar cal = Calendar.getInstance();
0834:
0835:                long offset = (cal.get(Calendar.ZONE_OFFSET) + cal
0836:                        .get(Calendar.DST_OFFSET));
0837:
0838:                outValue = (inTime + offset);
0839:
0840:                return outValue;
0841:            }
0842:
0843:            public final static long fromLocalizedTimeToUTCTime(long inTime) {
0844:
0845:                long outValue = 0;
0846:
0847:                Calendar cal = Calendar.getInstance();
0848:
0849:                long offset = (cal.get(Calendar.ZONE_OFFSET) + cal
0850:                        .get(Calendar.DST_OFFSET));
0851:
0852:                outValue = (inTime - offset);
0853:
0854:                return outValue;
0855:            }
0856:
0857:            public final static String fromTimeToString(long inTime) {
0858:
0859:                final String SOURCE_FORMAT = "yyyy-mm-dd hh:mm:ss";
0860:                final int SPACE_INDEX = SOURCE_FORMAT.indexOf(' ');
0861:
0862:                String outValue = null;
0863:
0864:                Timestamp ts = new Timestamp(inTime);
0865:
0866:                outValue = ts.toString();
0867:
0868:                outValue = outValue.substring(0, SOURCE_FORMAT.length());
0869:
0870:                outValue = outValue.replace(' ', 'T');
0871:
0872:                return outValue;
0873:            }
0874:
0875:            public final static long fromStringToTime(String inValue) {
0876:
0877:                try {
0878:                    long outValue = 0;
0879:
0880:                    inValue = inValue.substring(0, inValue.length() - 1);
0881:
0882:                    SimpleDateFormat sdf = new SimpleDateFormat(
0883:                            "yyyy-MM-dd'T'HH:mm:ss");
0884:
0885:                    outValue = sdf.parse(inValue).getTime();
0886:
0887:                    //outValue = fromLocalizedTimeToUTCTime(outValue);
0888:
0889:                    return outValue;
0890:                } catch (ParseException pe) {
0891:                    throw new UnexpectedSystemException(pe);
0892:                }
0893:            }
0894:
0895:            public final static String getStackTraceAsString(Exception inE) {
0896:
0897:                StringWriter outValue = new StringWriter();
0898:
0899:                PrintWriter pw = new PrintWriter(outValue);
0900:                inE.printStackTrace(pw);
0901:
0902:                return outValue.toString();
0903:            }
0904:
0905:            public final static byte[] fromStringToBytes(String inValue) {
0906:
0907:                try {
0908:                    byte outValue[] = null;
0909:
0910:                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
0911:                    OutputStreamWriter osw = new OutputStreamWriter(baos);
0912:
0913:                    osw.write(inValue, 0, inValue.length());
0914:                    osw.flush();
0915:                    baos.flush();
0916:
0917:                    outValue = baos.toByteArray();
0918:
0919:                    osw.close();
0920:                    baos.close();
0921:
0922:                    return outValue;
0923:                } catch (IOException ioe) {
0924:                    throw new UnexpectedSystemException(ioe);
0925:                }
0926:            }
0927:
0928:            //
0929:            public final static String fromBytesToString(byte inValue[]) {
0930:
0931:                try {
0932:                    StringBuffer outValue = new StringBuffer();
0933:
0934:                    ByteArrayInputStream bais = new ByteArrayInputStream(
0935:                            inValue);
0936:                    InputStreamReader isr = new InputStreamReader(bais);
0937:                    char buffer[] = new char[1024 * 16];
0938:                    int size = 0;
0939:                    while ((size = isr.read(buffer, 0, buffer.length)) != -1) {
0940:                        outValue.append(buffer, 0, size);
0941:                    }
0942:
0943:                    return outValue.toString();
0944:                } catch (IOException ioe) {
0945:                    throw new UnexpectedSystemException(ioe);
0946:                }
0947:            }
0948:
0949:            //
0950:            public final static ArrayList parseHandle(String inHandle,
0951:                    String inHc) {
0952:
0953:                ArrayList outValue = new ArrayList();
0954:
0955:                StringTokenizer st = new StringTokenizer(inHandle, inHc);
0956:                while (st.hasMoreTokens()) {
0957:                    outValue.add(st.nextToken());
0958:                }
0959:
0960:                return outValue;
0961:            }
0962:
0963:            public final static String reassembleHandle(ArrayList inList,
0964:                    String inHc) {
0965:
0966:                StringBuffer outValue = new StringBuffer();
0967:
0968:                for (int i = 0; i < inList.size(); i++) {
0969:
0970:                    outValue.append(inList.get(i));
0971:                    if (i < (inList.size() - 1)) {
0972:                        outValue.append(inHc);
0973:                    }
0974:                }
0975:
0976:                return outValue.toString();
0977:            }
0978:
0979:            //
0980:            public final static String normalizeCarriageReturns(String inValue) {
0981:
0982:                String outValue = inValue;
0983:
0984:                outValue = outValue.replaceAll("\\r\\n", "\n");
0985:                outValue = outValue.replaceAll("\\r", "\n");
0986:
0987:                return outValue;
0988:            }
0989:
0990:            //
0991:            public final static int hashCodeOrZero(Object inObject) {
0992:
0993:                int outValue = 0;
0994:
0995:                if (inObject != null) {
0996:                    outValue = inObject.hashCode();
0997:                }
0998:
0999:                return outValue;
1000:            }
1001:
1002:            public final static boolean equalsOrNull(Object inObj1,
1003:                    Object inObj2) {
1004:
1005:                boolean outValue = true;
1006:
1007:                if (inObj1 != null && inObj2 != null) {
1008:                    outValue = inObj1.equals(inObj2);
1009:                } else if (inObj1 == null && inObj2 == null) {
1010:                    outValue = true;
1011:                } else {
1012:                    outValue = false;
1013:                }
1014:
1015:                return outValue;
1016:            }
1017:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.