-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ImaInputStream.java
119 lines (101 loc) · 3.05 KB
/
ImaInputStream.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright (c) 2003 by Naohide Sano, All rights reserved.
*
* Programmed by Naohide Sano
*/
package vavi.sound.adpcm.ima;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.nio.ByteOrder;
import vavi.io.OutputEngineInputStream;
import static java.lang.System.getLogger;
/**
* IMA InputStream
*
* @author <a href="mailto:[email protected]">Naohide Sano</a> (nsano)
* @version 0.00 030816 nsano initial version <br>
*/
public class ImaInputStream extends FilterInputStream {
private static final Logger logger = getLogger(ImaInputStream.class.getName());
/**
* byte order is little endian
*/
public ImaInputStream(InputStream in,
int samplesPerBlock,
int channels,
int blockSize)
throws IOException {
this(in,
samplesPerBlock,
channels,
blockSize,
ByteOrder.BIG_ENDIAN);
}
/**
*
* @param in
* @param samplesPerBlock
* @param channels
* @param blockSize
* @param byteOrder
*/
public ImaInputStream(InputStream in,
int samplesPerBlock,
int channels,
int blockSize,
ByteOrder byteOrder)
throws IOException {
super(new OutputEngineInputStream(new ImaOutputEngine(in, samplesPerBlock, channels, blockSize, byteOrder)));
int bytesPerSample = 2;
int numSamples = Ima.getSamplesIn(in.available(), // TODO
channels,
blockSize,
samplesPerBlock);
this.available = numSamples * channels * bytesPerSample;
}
/** */
private int available;
@Override
public int available() throws IOException {
return available;
}
@Override
public int read() throws IOException {
available--;
return in.read();
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException("b");
} else if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException("off: " + off + ", len: " + len);
} else if (len == 0) {
return 0;
}
int c = read();
if (c == -1) {
return -1;
}
b[off] = (byte) c;
int i = 1;
try {
for (; i < len ; i++) {
c = read();
if (c == -1) {
break;
}
if (b != null) {
b[off + i] = (byte) c;
}
}
} catch (IOException e) {
logger.log(Level.ERROR, e.getMessage(), e);
}
return i;
}
}