128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
--------------------------------------------------------------------------------
/chapter4/FileChannelExample.java:
--------------------------------------------------------------------------------
1 | import java.io.*;
2 | import java.nio.*;
3 | import java.nio.channels.*;
4 |
5 | /**
6 | * Provides an easy mechinism for extracting the regex contents
7 | * of a file
8 | */
9 | public class FileChannelExample{
10 | public static void main(String args[]) throws IOException{
11 |
12 | String targetFile = "FileChannelExample.java";
13 |
14 | //if the user passed in a file to read, then use
15 | //that file instead
16 | if (args != null && args.length ==1)
17 | targetFile = args[0];
18 |
19 | //getDataUsingFileChannel(targetFile);
20 | //getDataUsingInputStream(targetFile);
21 | getDataUsingMappedFileChannel(targetFile);
22 |
23 | }
24 |
25 | /**
26 | * Extracts data from a file by using FileChannels
27 | * @param targetFile the name and path of the file to read
28 | * @throws IOException if anything goes wrong
29 | */
30 | public static void getDataUsingFileChannel(String targetFile)
31 | throws IOException{
32 | //open a connection to the source code for this
33 | //class
34 |
35 |
36 | //measure how long this process takes,
37 | //so take the start time
38 | long startTime = System.currentTimeMillis();
39 |
40 | FileInputStream fis =
41 | new FileInputStream(targetFile);
42 |
43 | //get a file channel
44 | FileChannel fc = fis.getChannel();
45 |
46 | //create a ByteBuffer that is large enough
47 | //and read the contents of the file into it
48 | ByteBuffer bb = ByteBuffer.allocate((int)fc.size());
49 |
50 | fc.read(bb);
51 |
52 | //set the ByteBuffer's position it it's begining
53 | bb.flip();
54 |
55 | //save the content of the file as a String
56 | String fileContent= new String(bb.array());
57 |
58 | //so take the end time
59 | long endTime = System.currentTimeMillis();
60 |
61 | long totalTime = (endTime - startTime);
62 |
63 | //release the FileChannel
64 | fc.close();
65 | fc = null;
66 | bb = null;
67 |
68 | //write out the contents of this file
69 | System.out.println("--getDataUsingFileChannel ");
70 |
71 | System.out.println("targetFile = " + targetFile);
72 |
73 | System.out.println("totalTime = " + totalTime);
74 | }
75 |
76 | /**
77 | * Extracts data from a file by using FileChannels
78 | * @param targetFile the name and path of the file to read
79 | * @throws IOException if anything goes wrong
80 | */
81 | public static void getDataUsingMappedFileChannel(String targetFile)
82 | throws IOException{
83 | //open a connection to the source code for this
84 | //class
85 |
86 |
87 | //measure how long this process takes,
88 | //so take the start time
89 | long startTime = System.currentTimeMillis();
90 |
91 | FileInputStream fis =
92 | new FileInputStream(targetFile);
93 |
94 | //get a file channel
95 | FileChannel fc = fis.getChannel();
96 |
97 | //map the byte buffer directly into memory
98 | fc.map(FileChannel.MapMode.READ_ONLY,0,(int)fc.size());
99 |
100 | //create a ByteBuffer that is large enough
101 | //and read the contents of the file into it
102 | ByteBuffer bb = ByteBuffer.allocate((int)fc.size());
103 |
104 | fc.read(bb);
105 |
106 | //set the ByteBuffer's position it it's begining
107 | bb.flip();
108 |
109 | //save the content of the file as a String
110 | String fileContent= new String(bb.array());
111 |
112 | //so take the end time
113 | long endTime = System.currentTimeMillis();
114 |
115 | long totalTime = (endTime - startTime);
116 |
117 | //release the FileChannel
118 | fc.close();
119 | fc = null;
120 |
121 | //write out the contents of this file
122 | System.out.println("--getDataUsingMappedFileChannel ");
123 |
124 | System.out.println("targetFile = " + targetFile);
125 |
126 | System.out.println("totalTime = " + totalTime);
127 | }
128 |
129 | /**
130 | * Extracts data from a file by using an InputStream
131 | * @param targetFile the name and path of the file to read
132 | * @throws IOException if anything goes wrong
133 | */
134 | public static void getDataUsingInputStream(String targetFile)
135 | throws IOException{
136 | //open a connection to the source code for this
137 | //class
138 |
139 | //measure how long this process takes,
140 | //so take the start time
141 | long startTime = System.currentTimeMillis();
142 |
143 | File file = new File(targetFile);
144 |
145 | //get the size of the file
146 | int fileSize = (int)file.length();
147 |
148 | FileInputStream fis =
149 | new FileInputStream(file);
150 |
151 | //create a byte[] that is large enough
152 | //and read the contents of the file into it
153 | byte[] byteArray = new byte[fileSize];
154 |
155 | fis.read(byteArray);
156 |
157 |
158 | //save the content of the file as a String
159 | String fileContent= new String(byteArray);
160 |
161 | //so take the end time
162 | long endTime = System.currentTimeMillis();
163 |
164 | long totalTime = (endTime - startTime);
165 |
166 | //release the file and stream
167 | fis.close();
168 | fis = null;
169 | file = null;
170 |
171 | //write out the contents of this file
172 | System.out.println("--getDataUsingInputStream");
173 |
174 | System.out.println("targetFile = " + targetFile);
175 | System.out.println("totalTime = " + totalTime);
176 | }
177 | }
178 |
--------------------------------------------------------------------------------
/docs/allclasses-frame.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | All Classes
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 | All Classes
19 |
20 |
21 |
126 |
127 |
128 |
129 |
--------------------------------------------------------------------------------
/util/RegexProperties.java:
--------------------------------------------------------------------------------
1 | import java.util.Properties;
2 | import java.util.regex.*;
3 | import java.util.*;
4 | import java.io.*;
5 | import java.nio.*;
6 | import java.nio.channels.*;
7 | import java.util.logging.Logger;
8 |
9 | /**
10 | * Provides a read-only extension of the java.util.properties file.
11 | * This class is unique because it is especially designed to read in
12 | * regular expressions which are not double delimited, as the String
13 | * class requires. Thus, \s is the actual string used to represent a
14 | * white space character, not \\s. Accordingly, this class does not allow
15 | * the regex patterns to be modified programmatically, nor does it
16 | * follow the normal property file convention for \n,\t, etc., or
17 | * multi line properties. Please see the documentation for the
18 | * load method
19 | */
20 | public class RegexProperties extends Properties{
21 | private static Logger log = Logger.getAnonymousLogger();
22 |
23 |
24 | /**
25 | * loads the file. @See load(FileInputStream inStream)
26 | *
27 | * @param String the name of the file to load
28 | * @throws IOException if there's an IO problem
29 | * @throws PatternSyntaxException if the File format isn't properly
30 | * formed, per the specification given above.
31 | */
32 |
33 | public void load(String inStream)
34 | throws IOException, PatternSyntaxException{
35 | load(new FileInputStream(inStream));
36 | }
37 | /**
38 | * Specialized property file for reading regular expressions
39 | * stored as properties. Reads a property list (key and
40 | * element pairs) from the input stream using a FileChannel,
41 | * thus allowing the usage of all characters. The stream is
42 | * assumed to be using the ISO 8859-1 character encoding.
43 | * Every property occupies one line of the input stream. Each
44 | * line is terminated by a line terminator (\n or \r or \r\n).
45 | * The entire contents of the file are read in.
46 | *
47 | A line that contains only white space or whose first
48 | * non-whitespace character is an ASCII # or ! is ignored
49 | * (thus, # or ! indicate comment lines).
50 | *
51 | * Every line other than a blank line or a comment line describes
52 | * one property to be added to the table. The key consists of
53 | * all the characters in the line starting with the first
54 | * non-whitespace character and up to, but not including,
55 | * the first ASCII =, :, or whitespace character. Any whitespace
56 | * after the key is skipped; if the first non-whitespace character
57 | * after the key is = or :, then it is ignored. White space character
58 | * after the = or ; are not skipped, and become part of the
59 | * value. This is a deliberate change from the default behavior of
60 | * the class, in order to support regular expressions, which may very
61 | * well need those characters. All remaining characters on the line
62 | * become part of the associated element string. If the last
63 | * character on the line is \, then the next line is not
64 | * treated as a continuation of the current line. Again, this is a
65 | * deliberate change from the default behavior of the class, in
66 | * order to support regular expressions.
67 | *
68 | * @param FileInputStream inStream the actual property file
69 | * @throws IOException if there's an IO problem
70 | * @throws PatternSyntaxException if the File format isn't properly
71 | * formed, per the specification given above.
72 | */
73 |
74 | public void load(FileInputStream inStream) throws IOException, PatternSyntaxException{
75 | // load the contents of the file
76 | FileChannel fc = inStream.getChannel();
77 |
78 | ByteBuffer bb = ByteBuffer.allocate((int)fc.size());
79 | fc.read(bb);
80 | bb.flip();
81 | String fileContent = new String(bb.array());
82 |
83 | //define a pattern that breaks the contents down line by line
84 | Pattern pattern = Pattern.compile("^(.*)$",Pattern.MULTILINE);
85 | Matcher matcher = pattern.matcher(fileContent);
86 |
87 | //iterate through the fileContent, line by line
88 | while (matcher.find()){
89 | //extract the relevant part of each file.
90 | //in this case, relevant means the characters
91 | //between the beginning of the line and it's end
92 | String line = matcher.group(1) ;
93 |
94 | //if the line is null or a comment, ignore it
95 | if (
96 | line != null &&
97 | !"".equals(line.trim()) &&
98 | !line.startsWith("#") &&
99 | !line.startsWith("!")
100 | )
101 | {
102 | String keyValue[] = null;
103 |
104 |
105 | //was the key- value entry split with the '='
106 | //character or the ':' character? Both are legal.
107 | if (line.indexOf("=") > 0 )
108 | keyValue = line.split("=",2);//only consume first =
109 | else
110 | keyValue = line.split(":",2);//only consume first :
111 |
112 | //final check that keyValue isn't null, because we
113 | //are going to be entering into a map, and trimming it
114 | if (keyValue != null)
115 | {
116 | super.put(keyValue[0].trim(),keyValue[1]);
117 | }
118 | }
119 | }
120 |
121 | fc = null;
122 | bb = null;
123 | }
124 | /**
125 | * Not supported. This is designed to be read only class
126 | * Throws UnsupportedOperationException.
127 | * @param String the key to be placed into this property
128 | * list.
129 | * @param String the value corresponding to key.
130 | * @throws UnsupportedOperationException
131 | *
132 | */
133 | public void store(FileOutputStream out, String header)
134 | throws UnsupportedOperationException
135 | {
136 | String msg = "unsupported for this class";
137 | throw new UnsupportedOperationException(msg);
138 | }
139 | /**
140 | * Not supported.
141 | * @param Object t - Mappings to be stored in this map.
142 | *
143 | * @returns nothing, since this call always throws an
144 | * UnsupportedOperationException.
145 | * @throws UnsupportedOperationException
146 | */
147 | public void putAll(Map t)
148 | {
149 | String msg = "unsupported for this class";
150 | throw new UnsupportedOperationException(msg);
151 | }
152 | }
153 |
--------------------------------------------------------------------------------
/chapter4/RegexProperties.java:
--------------------------------------------------------------------------------
1 |
2 | import java.util.Properties;
3 | import java.util.regex.*;
4 | import java.util.*;
5 | import java.io.*;
6 | import java.nio.*;
7 | import java.nio.channels.*;
8 | import java.util.logging.Logger;
9 |
10 |
11 | /**
12 | * Provides a read-only extension of the java.util.properties file.
13 | * This class is unique because it is especially designed to read in
14 | * regular expressions which are not double delimited, as the String
15 | * class requires. Thus, \s is the actual string used to represent a
16 | * whitespace character, not \\s. Accordingly, this class does not allow
17 | * the regex patterns to be modified programmatically, nor does it
18 | * follow the normal Property file convention for \n,\t, etc.,
19 | * or multiline properties. Please see the documentation for the
20 | * load method.
21 | */
22 | public class RegexProperties extends Properties{
23 | private static Logger log = Logger.getAnonymousLogger();
24 | private String fileContent=null;
25 |
26 | /**
27 | * loads the file. @See load(FileInputStream inStream)
28 | *
29 | * @param String the name of the file to load
30 | * @throws IOException if there's an IO problem
31 | * @throws PatternSyntaxException if the File format isn't properly
32 | * formed, per the specification given above.
33 | */
34 |
35 | public void load(String inStream)
36 | throws IOException, PatternSyntaxException{
37 | load(new FileInputStream(inStream));
38 | }
39 | /**
40 | * Specialized property file for reading regular expressions
41 | * stored as properties. Reads a property list (key and
42 | * element pairs) from the input stream using a FileChannel,
43 | * thus allowing the usage of all characters. The stream is
44 | * assumed to be using the ISO 8859-1 character encoding.
45 | * Every property occupies one line of the input stream. Each
46 | * line is terminated by a line terminator (\n or \r or \r\n).
47 | * The entire contents of the file are read in.
48 | *
49 | A line that contains only whitespace or whose first
50 | * non-whitespace character is an ASCII # or ! is ignored
51 | * (thus, # or ! indicate comment lines).
52 | *
53 | * Every line other than a blank line or a comment line describes
54 | * one property to be added to the table. The key consists of
55 | * all the characters in the line starting with the first
56 | * non-whitespace character and up to, but not including,
57 | * the first ASCII =, :, or whitespace character. Any whitespace
58 | * after the key is skipped; if the first non-whitespace character
59 | * after the key is = or :, then it is ignored. White space character
60 | * after the = or ; are not skipped, and become part of the
61 | * value. This is a deliberate change from the default behavior of
62 | * the class, in order to support regular expressions, which may very
63 | * well need those characters. All remaining characters on the line
64 | * become part of the associated element string. If the last
65 | * character on the line is \, then the next line is not
66 | * treated as a continuation of the current line. Again, this is a
67 | * deliberate change from the default behavior of the class, in
68 | * order to support regular expressions.
69 | *
70 | * @param FileInputStream inStream the actual property file
71 | * @throws IOException if there's an IO problem
72 | * @throws PatternSyntaxException if the File format isn't properly
73 | * formed, per the specification given above.
74 | */
75 |
76 | public void load(FileInputStream inStream) throws IOException, PatternSyntaxException{
77 | // load the contents of the file
78 | FileChannel fc = inStream.getChannel();
79 |
80 | ByteBuffer bb = ByteBuffer.allocate((int)fc.size());
81 | fc.read(bb);
82 | bb.flip();
83 | fileContent = new String(bb.array());
84 |
85 | //define a pattern that breaks the contents down line by line
86 | Pattern pattern = Pattern.compile("^(.*)$",Pattern.MULTILINE);
87 | Matcher matcher = pattern.matcher(fileContent);
88 |
89 | //iterate through the fileContent, line by line
90 | while (matcher.find()){
91 | //extract the relevant part of each file.
92 | //in this case, relevant means the characters
93 | //between the beginning of the line and it's end
94 | String line = matcher.group(1) ;
95 |
96 | //if the line is null or a comment, ignore it
97 | if (
98 | line != null &&
99 | !"".equals(line.trim()) &&
100 | !line.startsWith("#") &&
101 | !line.startsWith("!")
102 | )
103 | {
104 | String keyValue[] = null;
105 |
106 | //was the kay-value entry split with the '='
107 | //character or the ':' character? Both are legal.
108 | if (line.indexOf("=") > 0 )
109 | keyValue = line.split("=",2);
110 | else
111 | keyValue = line.split(":",2);
112 |
113 | //final check that keyValue isn't null, because we
114 | //are going to be entering into a map, and trimming it
115 | if (keyValue != null)
116 | {
117 | super.put(keyValue[0].trim(),keyValue[1]);
118 | }
119 | }
120 | }
121 |
122 | fc = null;
123 | bb = null;
124 | }
125 | /**
126 | * Not supported. This is designed to be read only class
127 | * Throws UnsupportedOperationException.
128 | * @param String the key to be placed into this property
129 | * list.
130 | * @param String the value corresponding to key.
131 | * @throws UnsupportedOperationException
132 | *
133 | */
134 | public void store(FileOutputStream out, String header)
135 | throws UnsupportedOperationException
136 | {
137 | String msg = "unsupported for this class";
138 | throw new UnsupportedOperationException(msg);
139 | }
140 | /**
141 | * Not supported.
142 | * @param Object t - Mappings to be stored in this map.
143 | *
144 | * @returns nothing, since this call always throws an
145 | * UnsupportedOperationException.
146 | * @throws UnsupportedOperationException
147 | */
148 | public void putAll(Map t)
149 | {
150 | String msg = "unsupported for this class";
151 | throw new UnsupportedOperationException(msg);
152 | }
153 |
154 | }
155 |
--------------------------------------------------------------------------------
/docs/serialized-form.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Serialized Form
8 |
9 |
10 |
11 |
12 |
13 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
77 | This API (Application Programming Interface) document has pages corresponding to the items in the navigation bar, described as follows.
78 | Package
79 |
80 |
81 |
82 | Each package has a page that contains a list of its classes and interfaces, with a summary for each. This page can contain four categories:
83 |
Interfaces (italic)
Classes
Exceptions
Errors
84 |
85 |
86 | Class/Interface
87 |
88 |
89 |
90 | Each class, interface, nested class and nested interface has its own separate page. Each of these pages has three sections consisting of a class/interface description, summary tables, and detailed member descriptions:
91 |
Class inheritance diagram
Direct Subclasses
All Known Subinterfaces
All Known Implementing Classes
Class/interface declaration
Class/interface description
92 |
93 |
Nested Class Summary
Field Summary
Constructor Summary
Method Summary
94 |
95 |
Field Detail
Constructor Detail
Method Detail
96 | Each summary entry contains the first sentence from the detailed description for that item. The summary entries are alphabetical, while the detailed descriptions are in the order they appear in the source code. This preserves the logical groupings established by the programmer.
97 |
98 | Tree (Class Hierarchy)
99 |
100 | There is a Class Hierarchy page for all packages, plus a hierarchy for each package. Each hierarchy page contains a list of classes and a list of interfaces. The classes are organized by inheritance structure starting with java.lang.Object. The interfaces do not inherit from java.lang.Object.
101 |
When viewing the Overview page, clicking on "Tree" displays the hierarchy for all packages.
When viewing a particular package, class or interface page, clicking "Tree" displays the hierarchy for only that package.
102 |
103 |
104 | Deprecated API
105 |
106 | The Deprecated API page lists all of the API that have been deprecated. A deprecated API is not recommended for use, generally due to improvements, and a replacement API is usually given. Deprecated APIs may be removed in future implementations.
107 |
108 | Index
109 |
110 | The Index contains an alphabetic list of all classes, interfaces, constructors, methods, and fields.
111 |
112 | Prev/Next
113 | These links take you to the next or previous class, interface, package, or related page.
114 | Frames/No Frames
115 | These links show and hide the HTML frames. All pages are available with or without frames.
116 |
117 |
118 | Serialized Form
119 | Each serializable or externalizable class has a description of its serialization fields and methods. This information is of interest to re-implementors, not to developers using the API. While there is no link in the navigation bar, you can get to this information by going to any serialized class and clicking "Serialized Form" in the "See also" section of the class description.
120 |
121 |
122 |
123 | This help file applies to API documentation generated using the standard doclet.
124 |
125 |
126 |