1 /* ZNet - Java Compression Layer for a new Socket Factory
2 Copyright (C) 1999, Free Software Rulez
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18 The author of this program may be contacted at morgiaclaudio@yahoo.it. */
19
20 package java.net;
21
22 import java.lang;
23 import java.io;
24
25 /**
26 * This is an utility class that provide an output method useful to print debug information.
27 * The usefulness is that it is driven by a global property: if you specify <i>-Ddebug=true</i>
28 * on the command line, the method {@link #p(String) D.p} prints the string otherwise it does
29 * nothing.
30 * @author <a href="mailto:morgiaclaudio@yahoo.it">Claudio Morgia</a>
31 * @version 1.0
32 */
33 public class D {
34 static boolean isDebug=false;
35
36 static {
37 if (System.getProperty("debug","false").equalsIgnoreCase("true"))
38 isDebug=true;
39 }
40
41 /**
42 * Use this method to print string to standard output based on the value of the global property
43 * <b>debug</b>.
44 * @param str the string to eventually print
45 */
46 static void p(String str) {
47 if (isDebug)
48 System.out.println(str);
49 }
50
51 /**
52 * This utility method entirely reads a file and returns the content as a byte array.
53 * @param name the file name or null if something wrong happened
54 * @return the file content in form of a byte array
55 */
56 static byte[] readFile(String name) {
57 int length;
58 try {
59 byte[] buf=new byte[16384];
60 ByteArrayOutputStream out=new ByteArrayOutputStream();
61 FileInputStream in=new FileInputStream(name);
62 while ((length=in.read(buf,0,buf.length))!=-1) {
63 out.write(buf,0,length);
64 }
65 return out.toByteArray();
66 } catch(Exception e) {
67 return null;
68 }
69 }
70
71 /**
72 * This utility method entirely writes a byte array to a named file.
73 * @param name the file name
74 * @param buf the content to write in form of a byte array
75 */
76 static void writeFile(String name,byte[] buf) {
77 try {
78 (new FileOutputStream(name)).write(buf);
79 } catch(Exception e) {}
80 }
81
82 /**
83 * This utility method checks the passed buffer for negative length or for total zeroed content.
84 */
85 static void check(byte[] buf,int len) {
86 if (len<0) {
87 D.p("inconsistent length");
88 return;
89 }
90 len--;
91 while ((len>=0) && buf[len]==0) len--;
92 if (len <0) D.p("blocco nullo");
93 }
94 }