Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Streams
- Writing Data to a Stream
- Workshop: Writing Bytes to an MP3 File
- Summary
- Q&A
- Quiz
- Questions
- Activities
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Workshop: Writing Bytes to an MP3 File
For this hour's workshop, you will make music, but by no stretch of the imagination can it be called beautiful music.
The project is to read in all of the bytes in an MP3 file and replace a small percentage of them with altered data.
Enter the text of Listing 12.3 into an editor and save the file as TrashMP3.java when you're done.
Example 12.3. The Full Text of TrashMP3.java
1: import java.io.*;
2:
3: public class TrashMP3 {
4: static int INTERFERENCE = 500;
5: static int RANDOMBYTE = 5;
6:
7: public static void main(String[] arguments) {
8: try {
9: File song = new File(arguments[0]);
10: FileInputStream file = new FileInputStream(song);
11: File trashedSong = new File("trashed.mp3");
12: FileOutputStream trash = new FileOutputStream(trashedSong);
13: boolean eof = false;
14: int count = 0;
15: System.out.println("Creating file ...");
16: while (!eof) {
17: int input = file.read();
18: if (input == -1)
19: eof = true;
20: else {
21: count++;
22: if (count % INTERFERENCE == 0) {
23: int newInput = input + RANDOMBYTE;
24: if (newInput > 255)
25: newInput = newInput - 255;
26: trash.write(newInput);
27: } else
28: trash.write(input);
29: }
30: }
31: file.close();
32: trash.close();
33: System.out.println("Done");
34: } catch (Exception e) {
35: System.out.println("Error — " + e.toString());
36: }
37: }
38: }
When you're done, compile the file with your Java compiler. The application requires one MP3 file that will be used for ill purpose. Though you won't be altering the file itself, you will be producing a version of it that may be quite painful to hear. The file Everlong.mp3 is not recommended for this purpose.
Run the application with the name of the MP3 file as a command-line argument, as in the following SDK example:
java TrashMP3 MuskratLove.mp3
The TrashMP3 program reads in all of the data in the MP3 file, in this example the song "Muskrat Love" by the Captain and Tennille. A file input stream associated with the file is created in Lines 9–10.
The MP3 file is read in the while loop contained in Lines 16–30. A boolean variable called eof is used as the condition of the loop. As long as it is equal to false, the loop will continue executing.
In Line 17, a single byte is read. If this byte is equal to -1, the end of the MP3 file has been reached, so eof is set to true in Lines 18–19, which will cause the loop to end.
The altered version of the MP3 will be written to its own file, which is given the name trashed.mp3 in Line 11. This file is associated with a file output stream in Line 12.
Data is written to the altered MP3 file in Lines 21–28. A count variable is used to determine when the next byte of altered data should be written.
The static variable INTERFERENCE determines how often a byte will be altered. It is set to 500 when it is created in Line 4, so when you run the application, every 500th byte will be trashed.
The static variable RANDOMBYTE determines how much the byte will be altered. In Line 23, the value of this variable is added to the byte that was read from the original file. If this value exceeds 255, it cannot be used as a byte, so Lines 24–25 make sure this doesn't happen.
The altered byte is written to the new MP3 file in Line 26. Unaltered bytes are written in Line 28.
If you have an MP3 player, you can play your handiwork by opening the folder that contains trashed.mp3 and double-clicking its icon. The MP3 format is usually pretty resilient in the face of unauthorized tampering like this, so you probably will be able to hear the song with a few wrong notes, odd moments of bad tempo, and lots of "gleep!" and "glorp!" sound effects.
Summary | Next Section

Account Sign In
View your cart