Exercises
Write a utility method for copying all of an InputStream to an OutputStream, without using any temporary files. Provide another solution, without a loop, using operations from the Files class, using a temporary file.
Write a program that reads a text file and produces a file with the same name but extension .toc, containing an alphabetized list of all words in the input file together with a list of line numbers in which each word occurs. Assume that the file’s encoding is UTF-8.
Write a program that reads a file containing text and, assuming that most words are English, guesses whether the encoding is ASCII, ISO 8859-1, UTF-8, or UTF-16, and if the latter, which byte ordering is used.
Using a Scanner is convenient, but it is a bit slower than using a BufferedReader. Read in a long file a line at a time, counting the number of input lines, with (a) a Scanner and hasNextLine/nextLine, (b) a BufferedReader and readLine, (c) a BufferedReader and lines. Which is the fastest? The most convenient?
When an encoder of a Charset with partial Unicode coverage can’t encode a character, it replaces it with a default—usually, but not always, the encoding of "?". Find all replacements of all available character sets that support encoding. Use the newEncoder method to get an encoder, and call its replacement method to get the replacement. For each unique result, report the canonical names of the charsets that use it.
The BMP file format for uncompressed image files is well documented and simple. Using random access, write a program that reflects each row of pixels in place, without writing a new file.
Look up the API documentation for the MessageDigest class and write a program that computes the SHA-512 digest of a file. Feed blocks of bytes to the MessageDigest object with the update method, then display the result of calling digest. Verify that your program produces the same result as the sha512sum utility.
Write a utility method for producing a ZIP file containing all files from a directory and its descendants.
Using the URLConnection class, read data from a password-protected web page with “basic” authentication. Concatenate the user name, a colon, and the password, and compute the Base64 encoding:
String input = username + ":" + password; String encoding = Base64.getEncoder().encodeToString( input.getBytes(StandardCharsets.UTF_8));
Set the HTTP header Authorization to the value "Basic " + encoding. Then read and print the page contents.
Using a regular expression, extract all decimal integers (including negative ones) from a string into an ArrayList<Integer> (a) using find, and (b) using split. Note that a + or - that is not followed by a digit is a delimiter.
Using regular expressions, extract the directory path names (as an array of strings), the file name, and the file extension from an absolute or relative path such as /home/cay/myfile.txt.
Come up with a realistic use case for using group references in Matcher.replaceAll and implement it.
Implement a method that can produce a clone of any serializable object by serializing it into a byte array and deserializing it.
Implement a serializable class Point with instance variables for x and y. Write a program that serializes an array of Point objects to a file, and another that reads the file.
Continue the preceding exercise, but change the data representation of Point so that it stores the coordinates in an array. What happens when the new version tries to read a file generated by the old version? What happens when you fix up the serialVersionUID? Suppose your life depended upon making the new version compatible with the old. What could you do?
Which classes in the standard Java library implement Externalizable? Which of them use writeReplace/readResolve?
Unzip the API source and investigate how the LocalDate class is serialized. Why does the class define writeExternal and readExternal methods even though it doesn’t implement Externalizable? (Hint: Look at the Ser class. Why does the class define a readObject method? How could it be invoked?