View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * @author lutecer
50   *
51   */
52  public class SequenceAudioInputStream extends AudioInputStream
53  {
54      private List<AudioInputStream> _audioInputStreamList;
55      private int _nCurrentStream;
56  
57      /**
58       *
59       * @param audioFormat
60       *            the audio Format
61       * @param audioInputStreams
62       *            the audio InputStreams
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       * @return the audio inputStream
74       */
75      private AudioInputStream getCurrentStream( )
76      {
77          return _audioInputStreamList.get( _nCurrentStream );
78      }
79  
80      /**
81       *
82       * @return true if mcurrent stream < audioInputStreamList size
83       */
84      private boolean advanceStream( )
85      {
86          _nCurrentStream++;
87  
88          boolean bAnotherStreamAvailable = ( _nCurrentStream < _audioInputStreamList.size( ) );
89  
90          return bAnotherStreamAvailable;
91      }
92  
93      /**
94       * @return frame length
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      * @return the byte read
121      * @throws IOException
122      *             the IOException
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              * The end of the current stream has been signaled. We try to advance to the next stream.
133              */
134             boolean bAnotherStreamAvailable = advanceStream( );
135 
136             if ( bAnotherStreamAvailable )
137             {
138                 /*
139                  * There is another stream. We recurse into this method to read from it.
140                  */
141                 return read( );
142             }
143             else
144             {
145                 /*
146                  * No more data. We signal EOF.
147                  */
148                 return -1;
149             }
150         }
151         else
152         {
153             /*
154              * The most common case: We return the byte.
155              */
156             return nByte;
157         }
158     }
159 
160     /**
161      * @param abData
162      *            the data
163      * @param nOffset
164      *            the offset
165      * @param nLength
166      *            the length
167      * @return the bytes read
168      * @throws IOException
169      *             the IOException
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              * The end of the current stream has been signaled. We try to advance to the next stream.
180              */
181             boolean bAnotherStreamAvailable = advanceStream( );
182 
183             if ( bAnotherStreamAvailable )
184             {
185                 /*
186                  * There is another stream. We recurse into this method to read from it.
187                  */
188                 return read( abData, nOffset, nLength );
189             }
190             else
191             {
192                 /*
193                  * No more data. We signal EOF.
194                  */
195                 return -1;
196             }
197         }
198         else
199         {
200             /*
201              * The most common case: We return the length.
202              */
203             return nBytesRead;
204         }
205     }
206 
207     /**
208      * @return the current stream available
209      * @throws IOException
210      *             the IOException
211      */
212     public int available( ) throws IOException
213     {
214         return getCurrentStream( ).available( );
215     }
216 }
217 /** * SequenceAudioInputStream.java ** */