KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > faceless > pdf > PDFSound


1 // $Id: PDFSound.java,v 1.6 2004/05/18 12:56:04 mike Exp $
2

3 package org.faceless.pdf;
4
5 import java.util.*;
6 import java.io.*;
7
8 /**
9  * A PDFSound represents an audio sample in a PDF document.
10  * Valid types of audio sample include:
11  * <ul>
12  * <li>RIFF file (Microsoft Windows <code>.wav</code> file)</li>
13  * <li>AU file (Sun/NeXT format)</li>
14  * <li>AIFF file (Macintosh format)</li>
15  * <li>AIFF-C file (Compressed Macintosh format)</li>
16  * </ul>
17  * Note that although <i>in theory</i> all these sound
18  * formats are allowed in PDF documents, our own tests have
19  * found incomplete support in Acrobat reader for anything
20  * other than WAV audio files.
21  * <p>
22  * Sound is unlikely to work in anything other than Adobes
23  * own Acrobat products on Windows and Macintosh platforms.
24  * Your mileage may vary.
25  * </p>
26  *
27  * @since 1.1
28  */

29 public class PDFSound extends PeeredObject
30 {
31     final org.faceless.pdf2.PDFSound sound;
32
33     Object JavaDoc getPeer()
34     {
35         return sound;
36     }
37
38     PDFSound(org.faceless.pdf2.PDFSound sound)
39     {
40         this.sound=sound;
41     }
42
43     /**
44      * Create a new PDFSound from the specified input file. The file may
45      * be a RIFF (.wav), AIFF, AIFF-C or Sun .au audio file (although see
46      * our note in the class header).
47      * @throws IllegalArgumentException if the file format is invalid
48      */

49     public PDFSound(InputStream in)
50         throws IllegalArgumentException JavaDoc, IOException
51     {
52     sound = new org.faceless.pdf2.PDFSound(in);
53     }
54
55     /**
56      * Return the number of samples/second this sound is played at.
57      * Common values are 8000, 11025 or 22050
58      * @since 1.1.12
59      */

60     public int getRate()
61     {
62     return sound.getRate();
63     }
64
65     /**
66      * Return the type of sound file this sound represents. Values are
67      * either "RIFF" for Microsoft WAV files, "AU" for Sun Audio files,
68      * "AIFF" or "AIFC" for Apple AIFF or AIFF (compressed) files
69      * respectively. If the type of file cannot be recognised, returns
70      * <tt>null</tt>
71      * @since 1.1.12
72      */

73     public String JavaDoc getType()
74     {
75     return sound.getType();
76     }
77
78     /**
79      * Return the sampled sound file encapsulated by this PDFSound object
80      * @since 1.1.12
81      */

82     public byte[] getStream()
83     {
84     try {
85         ByteArrayOutputStream out = new ByteArrayOutputStream();
86         InputStream in = sound.getStream();
87         byte[] b = new byte[1024];
88         int flen;
89         while ((flen=in.read(b))>=0) {
90         out.write(b, 0, flen);
91         }
92         return out.toByteArray();
93     } catch (IOException e) { throw new Error JavaDoc("IOException writing to byte array: "+e); }
94     }
95
96     /**
97      * Set the XML metadata associated with this object. See
98      * {@link PDF#setMetaData} for more information.
99      * @param xmldata the XML data to embed into the document, or <tt>null</tt> to clear any existing metadata. No validation is performed on this input.
100      * @since 1.1.12
101      */

102     public void setMetaData(String JavaDoc xmldata)
103     {
104     sound.setMetaData(xmldata);
105     }
106
107     /**
108      * Return any XML metadata associated with this object. See the
109      * {@link PDF#getMetaData} for more information
110      * @return a {@link java.io.Reader} containing the source of the XML or <tt>null</tt> if no metadata is available.
111      * @throws IOException if the metadata can't be extracted
112      * @since 1.1.12
113      */

114     public Reader getMetaData()
115         throws IOException
116     {
117     return sound.getMetaData();
118     }
119 }
120
Popular Tags