Cross Reference: AlawEncoderSync.java
xref
: /
openjdk7
/
jdk
/
test
/
javax
/
sound
/
sampled
/
FileWriter
/
AlawEncoderSync.java
Home
History
Annotate
Line#
Navigate
Download
Search
only in
./
3629
N/A
/**
3629
N/A
*
@test
3629
N/A
*
@bug
6938426
3629
N/A
*
@summary
Tests that Alaw encoder works properly in multithreaded environment
3629
N/A
*
@author
Alex Menkov
3629
N/A
*/
3629
N/A
3629
N/A
import
java
.
io
.
ByteArrayInputStream
;
3629
N/A
import
java
.
io
.
ByteArrayOutputStream
;
3629
N/A
import
java
.
io
.
InputStream
;
3629
N/A
import
java
.
util
.
Arrays
;
3629
N/A
import
javax
.
sound
.
sampled
.
AudioFormat
;
3629
N/A
import
javax
.
sound
.
sampled
.
AudioInputStream
;
3629
N/A
import
javax
.
sound
.
sampled
.
AudioSystem
;
3629
N/A
3629
N/A
public
class
AlawEncoderSync
{
3629
N/A
3629
N/A
static
final
int
THREAD_COUNT
=
20
;
3629
N/A
3629
N/A
static
final
AudioFormat
pcmFormat
=
new
AudioFormat
(
8000f
,
16
,
2
,
true
,
false
);
3629
N/A
static
final
int
STREAM_LENGTH
=
10
;
// in seconds
3629
N/A
static
byte
[]
pcmBuffer
;
3629
N/A
static
final
AudioFormat
alawFormat
3629
N/A
=
new
AudioFormat
(
AudioFormat
.
Encoding
.
ALAW
,
8000f
,
8
,
2
,
2
,
8000f
,
false
);
3629
N/A
3629
N/A
static
final
ConversionThread
[]
threads
=
new
ConversionThread
[
THREAD_COUNT
];
3629
N/A
3629
N/A
public
static
void
main
(
String
[]
args
) {
3629
N/A
preparePCMBuffer
();
3629
N/A
log
(
"pcmStream size: "
+
pcmBuffer
.
length
);
3629
N/A
3629
N/A
for
(
int
i=
0
; i<
THREAD_COUNT
; i++) {
3629
N/A
threads
[i] =
new
ConversionThread
(i);
3629
N/A
threads
[i].
start
();
3629
N/A
}
3629
N/A
3629
N/A
for
(
int
i=
1
; i<
THREAD_COUNT
; i++) {
3629
N/A
try
{
3629
N/A
threads
[i].
join
();
3629
N/A
}
catch
(
InterruptedException
ex
) {
3629
N/A
log
(
"Main thread was interrupted, exiting."
);
3629
N/A
return
;
3629
N/A
}
3629
N/A
}
3629
N/A
3629
N/A
int
failed
=
0
;
3629
N/A
log
(
"comparing result arrays..."
);
3629
N/A
for
(
int
i=
1
; i<
THREAD_COUNT
; i++) {
3629
N/A
if
(!
Arrays
.
equals
(
threads
[
0
].
resultArray
,
threads
[i].
resultArray
)) {
3629
N/A
failed
++;
3629
N/A
log
(
"NOT equals: 0 and "
+ i);
3629
N/A
}
3629
N/A
}
3629
N/A
if
(
failed
>
0
) {
3629
N/A
throw
new
RuntimeException
(
"test FAILED"
);
3629
N/A
}
3629
N/A
log
(
"test PASSED."
);
3629
N/A
}
3629
N/A
3629
N/A
3629
N/A
static
void
preparePCMBuffer
() {
3629
N/A
pcmBuffer
=
new
byte
[
STREAM_LENGTH
* (
int
)
pcmFormat
.
getSampleRate
()
3629
N/A
* (
pcmFormat
.
getSampleSizeInBits
() /
8
) *
pcmFormat
.
getChannels
()];
3629
N/A
for
(
int
i=
0
; i<
pcmBuffer
.
length
; i++) {
3629
N/A
pcmBuffer
[i] = (
byte
)(
Math
.
random
() *
256.0
-
128.0
);
3629
N/A
}
3629
N/A
}
3629
N/A
3629
N/A
static
AudioInputStream
createPCMStream
() {
3629
N/A
InputStream
byteStream
=
new
ByteArrayInputStream
(
pcmBuffer
);
3629
N/A
return
new
AudioInputStream
(
byteStream
,
pcmFormat
,
AudioSystem
.
NOT_SPECIFIED
);
3629
N/A
}
3629
N/A
3629
N/A
static
class
ConversionThread
extends
Thread
{
3629
N/A
public
final
int
num
;
3629
N/A
public
byte
[]
resultArray
=
null
;
3629
N/A
public
ConversionThread
(
int
num
) {
3629
N/A
this
.
num
=
num
;
3629
N/A
}
3629
N/A
@
Override
3629
N/A
public
void
run
() {
3629
N/A
log
(
"ConversionThread["
+
num
+
"] started."
);
3629
N/A
try
{
3629
N/A
InputStream
inStream
=
new
ByteArrayInputStream
(
pcmBuffer
);
3629
N/A
3629
N/A
AudioInputStream
pcmStream
=
new
AudioInputStream
(
3629
N/A
inStream
,
pcmFormat
,
AudioSystem
.
NOT_SPECIFIED
);
3629
N/A
AudioInputStream
alawStream
=
AudioSystem
.
getAudioInputStream
(
alawFormat
,
pcmStream
);
3629
N/A
3629
N/A
ByteArrayOutputStream
outStream
=
new
ByteArrayOutputStream
();
3629
N/A
int
read
=
0
;
3629
N/A
byte
[]
data
=
new
byte
[
4096
];
3629
N/A
while
((
read
=
alawStream
.
read
(
data
)) != -
1
) {
3629
N/A
outStream
.
write
(
data
,
0
,
read
);
3629
N/A
}
3629
N/A
alawStream
.
close
();
3629
N/A
resultArray
=
outStream
.
toByteArray
();
3629
N/A
}
catch
(
Exception
ex
) {
3629
N/A
log
(
"ConversionThread["
+
num
+
"] exception:"
);
3629
N/A
log
(
ex
);
3629
N/A
}
3629
N/A
log
(
"ConversionThread["
+
num
+
"] completed."
);
3629
N/A
}
3629
N/A
}
3629
N/A
3629
N/A
static
void
log
(
String
s) {
3629
N/A
System
.
out
.
println
(s);
3629
N/A
}
3629
N/A
3629
N/A
static
void
log
(
Exception
ex
) {
3629
N/A
ex
.
printStackTrace
(
System
.
out
);
3629
N/A
}
3629
N/A
}