1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.captcha.modules.jcaptcha.service.sound;
35
36 import java.io.ByteArrayInputStream;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.Iterator;
41 import java.util.List;
42
43 import javax.sound.sampled.AudioFormat;
44 import javax.sound.sampled.AudioInputStream;
45 import javax.sound.sampled.AudioSystem;
46
47
48
49
50
51
52 public class SequenceAudioInputStream extends AudioInputStream
53 {
54 private List<AudioInputStream> _audioInputStreamList;
55 private int _nCurrentStream;
56
57
58
59
60
61
62
63
64 public SequenceAudioInputStream( AudioFormat audioFormat, Collection<AudioInputStream> audioInputStreams )
65 {
66 super( new ByteArrayInputStream( new byte [ 0] ), audioFormat, AudioSystem.NOT_SPECIFIED );
67 _audioInputStreamList = new ArrayList<AudioInputStream>( audioInputStreams );
68 _nCurrentStream = 0;
69 }
70
71
72
73
74
75 private AudioInputStream getCurrentStream( )
76 {
77 return _audioInputStreamList.get( _nCurrentStream );
78 }
79
80
81
82
83
84 private boolean advanceStream( )
85 {
86 _nCurrentStream++;
87
88 boolean bAnotherStreamAvailable = ( _nCurrentStream < _audioInputStreamList.size( ) );
89
90 return bAnotherStreamAvailable;
91 }
92
93
94
95
96 public long getFrameLength( )
97 {
98 long lLengthInFrames = 0;
99 Iterator<AudioInputStream> streamIterator = _audioInputStreamList.iterator( );
100
101 while ( streamIterator.hasNext( ) )
102 {
103 AudioInputStream stream = streamIterator.next( );
104 long lLength = stream.getFrameLength( );
105
106 if ( lLength == AudioSystem.NOT_SPECIFIED )
107 {
108 return AudioSystem.NOT_SPECIFIED;
109 }
110 else
111 {
112 lLengthInFrames += lLength;
113 }
114 }
115
116 return lLengthInFrames;
117 }
118
119
120
121
122
123
124 public int read( ) throws IOException
125 {
126 AudioInputStream stream = getCurrentStream( );
127 int nByte = stream.read( );
128
129 if ( nByte == -1 )
130 {
131
132
133
134 boolean bAnotherStreamAvailable = advanceStream( );
135
136 if ( bAnotherStreamAvailable )
137 {
138
139
140
141 return read( );
142 }
143 else
144 {
145
146
147
148 return -1;
149 }
150 }
151 else
152 {
153
154
155
156 return nByte;
157 }
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171 public int read( byte [ ] abData, int nOffset, int nLength ) throws IOException
172 {
173 AudioInputStream stream = getCurrentStream( );
174 int nBytesRead = stream.read( abData, nOffset, nLength );
175
176 if ( nBytesRead == -1 )
177 {
178
179
180
181 boolean bAnotherStreamAvailable = advanceStream( );
182
183 if ( bAnotherStreamAvailable )
184 {
185
186
187
188 return read( abData, nOffset, nLength );
189 }
190 else
191 {
192
193
194
195 return -1;
196 }
197 }
198 else
199 {
200
201
202
203 return nBytesRead;
204 }
205 }
206
207
208
209
210
211
212 public int available( ) throws IOException
213 {
214 return getCurrentStream( ).available( );
215 }
216 }
217