HttpのStreamingデータ配信処理でトラフィック量が問題となった。
解決方法として、バイナリデータを圧縮し配信することで解決した。
圧縮率はデータにもよるが60%~70%は圧縮された。
以下、Javaコードとなる。
■圧縮
/**
* バイナリを圧縮します
* @param binary 圧縮対象バイナリ
* @return 圧縮されたバイナリ
*/
public static byte[] compress (byte[] binary) {
// 書き込まれるメモリストリーム
ByteArrayOutputStream outBin = new ByteArrayOutputStream() ;
try {
// Zip書込み
ZipOutputStream outZip = new ZipOutputStream( outBin ) ;
// ダミーエントリ
ZipEntry zEnt = new ZipEntry( System.currentTimeMillis() + "" ) ;
outZip.putNextEntry( zEnt ) ;
// 書込み
outZip.write( binary, 0, binary.length) ;
outZip.closeEntry();
outZip.flush();
outZip.close() ;
} catch (IOException e) {
e.printStackTrace();
}
return outBin.toByteArray();
}
■解凍
/**
* バイナリを解凍します
* @param binary 圧縮されたバイナリ
* @param size 圧縮前データサイズ
* @return 解凍されたバイナリデータ
* @throws IOException
*/
public static byte[] decompress(byte[] binary, int size) throws IOException {
byte [] buf = new byte[ size ] ;
// Zip 読み込み
ZipInputStream inZip = new ZipInputStream( new ByteArrayInputStream( binary ) ) ;
for (java.util.zip.ZipEntry ze = inZip.getNextEntry(); ze != null; ze = inZip.getNextEntry()) {
if (ze.isDirectory()) continue;
int pos = 0 ;
int len = 0;
while ( true ){
len = inZip.read( buf, pos, size - pos ) ;
pos += len ;
if ( pos >= buf.length) {
break ;
}
}
}
return buf;
}