㈠ as3如何傳遞ByteArray給js
Socket數據獲取
/**
* 當客戶端給服務端傳遞信息處理,這里將自動處理內容分割
* @param e
*/
protected function onSocketData(e:ProgressEvent):void
{
if(socket.bytesAvailable == 0)
return;
//將數據儲存在緩存區
var byte:ByteArray = new ByteArray();
socket.readBytes(byte,0,e.bytesTotal);
pushCache(byte);
parsingCache();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
當Socket給我們傳輸數據的時候,我們可以偵聽ProgressEvent事件進行載入讀取。Socket的傳輸原理就跟下載文件一樣,你在下載服務端那邊的數據,只是數據是自定義的,但數據是按循序而來的。
將數據添加到緩存
/**
* 寫入緩存區
* @param byte
*/
public function pushCache(byte:ByteArray):void
{
if(_cache == null)
{
_cache = byte;
}
else
{
_cache.position = _cache.length;
_cache.writeBytes(byte);
byte.clear();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
下載數據,意味著並不是每次下載的數據都是完整的,可能會有錯位,數據不完整的情況。所以需要將下載到的數據,裝到一個ByteArray里作為緩存,方便下一次獲取使用。
自定義發送內容格式
/**
* 發送信息到服務端,或者客戶端
* @param data
*/
public function send(data:Object):void
{
//新建一個二進制內容
var byte:ByteArray = new ByteArray();
//寫入Object
byte.writeObject(data);
//壓縮二進制內容
byte.compress();
//傳輸內容長度
socket.writeShort(byte.length);
//傳輸內容
socket.writeBytes(byte);
//確定內容寫入
socket.flush();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
這里有一個自定義方法send,裡面的data就是我要發送的內容,但不管是什麼內容,都將它裝載到一個ByteArray數組里。重要的是,ByteArray自帶有compress的壓縮功能,這意味著傳輸的寬頻需求進行了壓縮。這里的傳輸順序是:內容長度(Short):內容(ByteArray)。
解析接收到的內容
/**
* 解析分發數據包
*/
public function parsingCache():void
{
//判斷緩存是否存在
if(!_cache)
return;
//初始化緩存的讀取位置
var par:int = 0;
_cache.position = par;
//如果緩存的位元組大於2,因為Short佔位2個字元
while(_cache.bytesAvailable > 2){
//解析時,讀取內容長度,這里會取出2個字元
var len:uint = _cache.readShort();
//判斷緩存的可用長度是否足夠讀取內容
if(_cache.bytesAvailable < len)
{
//當緩存不足讀取時,中斷讀取
break;
}
else
{
//當緩存足夠可讀取時,將當前的緩存位置增加兩個字元,跳過Short內容。
_cache.position = par + 2;
//讀取其內容
var data:ByteArray = new ByteArray();
_cache.readBytes(data,0,len);
//返回數據
backData(data);
//緩存位置刷新至下一個
par += 2 + len;
_cache.position = par;
}
}
//判斷緩存的位置是否做了刷新,如果有刷新,意味著讀取過內容
if(par != 0)
{
//判斷緩存的可用長度
if(_cache.bytesAvailable > 0)
{
//存在可用的長度時,截取par後面未使用的新數據,以便下一次使用
var newbyte:ByteArray = new ByteArray();
_cache.position = par;
_cache.readBytes(newbyte,0,_cache.bytesAvailable);
_cache.clear();
_cache = newbyte;
}
else
{
//如果數據本身不存在可用數據,那麼可以進行釋放
_cache.clear();
_cache = null;
}
}
}
//數據返回,進行解析
private function backData(data:ByteArray):void
{
try{
//還原讀取點
data.position = 0;
//解壓數據
data.uncompress();
//讀取之前定義好的Object內容
var ob:Object = data.readObject();
if(ob)
{
//自定義操作
if(dataFunc != null)
dataFunc(ob);
}
}
catch(e:Error)
{
trace("解析出現異常");
}
}
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
這里做了ByteArray的解析處理,根據其內容長度,讀取內容。Short占兩個字元,因此跳過固定為2值。有一個固定的規則,二進制的內容都是自由定義的。
㈡ java ByteArrayOutputStream從壓縮文件中讀取到位元組數組,轉換成字元串後再轉換成數組發現數據不一樣了
您好, 提問者:
ISO8859-1是佔1個位元組。
而UTF-8的漢字是佔三個位元組。
GBK的漢字的是占兩個位元組,當然不一樣了。
//轉換
new String(splitData.getBytes("ISO8859-1"),"UTF-8");
㈢ byte數組怎麼存放到Json中傳遞
json 是一種很簡潔的協議,但可惜的是,它只能傳遞基本的數型(int,long,string等),但不能傳遞byte類型。如果想要傳輸圖片等二進制文件的話,是沒辦法直接傳輸。
本文提供一種思路給大家參考,讓大家可以在json傳輸二進制文件,如果大家有這個需求又不知怎麼實現的話,也許本文能夠幫到你。思想適用於所有語言,本文以Java實現,相信大家很容易就能轉化為自己懂得語言。
思路
1. 讀取二進制文件到內存
2. 用Gzip壓縮一下。畢竟是在網路傳輸嘛,當然你也可以不壓縮。
3. 用Base64 把byte[] 轉成字元串
㈣ 如何將BYTE數組初始化
文件格式:mysql-noinstall-version-win32.zip
在安裝之前,請下載mysql-noinstall-5.1.31-win32.zip和mysql-noinstall-5.0.77-win32.zip這兩個文件。
由於MySQL一直在升級,所以當您閱讀本文時,或許在其官方網站上已提供了新的正式版下載,因此,請進入MySQL下載頁面http://dev.mysql.com/downloads/ 查找當前的最新可用版本。
另外,在下載過程中,會提示你需要注冊為網路用戶,這是一個很簡短的過程,它需要您提供一個有效的email地址。當然,如果不注冊的話,你也可以跳過此步,直接下載既可。
2、解壓縮並放置到你期望的目錄下
(建議放置在不包含空格的目錄下,如E:\mysql )
為了方便,修改解壓縮後的文件夾名為mysql,並放置在C:\mysql (因為是配置文件里默認的路徑。)
將下載後的mysql-noinstall-5.0.77-win32.zip解壓到%MySQL_HOME%(此處%MySQL_HOME%表示MySQL的安裝路徑,如我將其解壓至E:\dev\,為了方便,修改解壓縮後的文件夾名為mysql-5.1.31)。另外,為了便於日後備份和升級,最好把安裝文件中的data目錄存放在其它地方。此處,我在E盤新建一個目錄"MySQL5.1Data",然後把"E:\dev\mysql-5.1.31"目錄下的"data"子目錄剪切到"E:\MySQL5.1Data"目錄中。
3、創建一個配置文件
對於伺服器每次啟動都用到的選項和對MySQL服務的一些設置,你會發現使用配置文件來指定MySQL配置非常方便。
MySQL配置文件,一般放在Windows系統目錄中,如C:\WINDOWS 或C:\WINNT ,名為my.ini 。但是,此處我們是安裝兩個MySQL,因此將其放在MySQL解壓縮後的文件夾中。在該文件夾中,一般包含5個MySQL自帶的配置文件,my-small.ini、my-medium.ini、my-large.ini、my-huge.ini和my-innodb-heavy-4G.ini,請你根據自己機器的內存大小,選擇其一,並把它重新命名為my.ini用作基本配置文件。
配置文件中的一些參數,需要根據安裝目錄的不同,做相應的修改,如
[WinMySQLAdmin]
Server=E:/dev/mysql-5.1.31/bin/mysqld.exe
[mysqld]
basedir=E:/dev/mysql-5.1.31
datadir=E:/MySQL5.1Data/data
default-character-set=gbk
port=3306
[client]
default-character-set=gbk
port=3306
4、將MySQL加入到Windows 的服務中
打開MS-DOS窗口,進入DOS環境,切換到"%MySQL_HOME%\bin"目錄
運行:
%MySQL_HOME%\bin>mysqld --install mysql5.1
(此時,在運行中輸入"services.msc"或者打開"控制面板"->"管理工具"->"服務",可以看到服務列表中存在"MySQL5.1"服務。如果不希望它自動運行,也可以改為手動,這樣,當你不用它的時候,也可以節省內存。)
5、啟動和停止MySQL服務
%MySQL_HOME%\bin>net start mysql
或者進入DOS環境,進入如下目錄運行:%MySQL_HOME%\bin\mysqld.exe
(或直接雙擊mysqld.exe文件),即可啟動MySQL服務
%MySQL_HOME%\bin>net stop mysql(停止MySQL服務)
註:如果要卸載MySQL服務,請運行%MySQL_HOME%\bin>mysqld --remove
(如果你不再需要MySQL了,請先停掉MySQL的服務,然後再把MySQL的服務卸載掉,最後刪除MySQL安裝目錄即可。)
6、進入MySQL。
%MySQL_HOME%\bin\mysql -uroot -p (密碼為空)
(如果出現ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)提示,表示MySQL服務沒有被啟動,其主要的原因在於,配置文件寫的不對。
如果出現ERROR 1045 (28000): Access denied for user [email=]'root'@'localhost'[/email] (using password: NO)提示,表示MySQL服務已經啟動,但是你所輸入的密碼不正確。
如果出現1067的錯誤:A system error has occurred.System error 1067 has occurred.The process terminated unexpectedly.提示,無法啟動MySQL服務,進程意外終止,表示你的配置文件有問題,請檢查my.ini裡面的路勁是否寫錯了或者試著刪除MySQL5.1Data文件中的LOG文件。)
mysql>select version(); (顯示目前你所安裝的MySQL的資料庫版本信息)
如果上面的所有操作步驟均正常的話,那麼恭喜你,你已安裝好了MySQL 5.1,下面我們繼續安裝MySQL 5.0.
(二)安裝MySQL 5.0
安裝MySQL 5.0的步驟與安裝MySQL 5.1的步驟類似,其中,有兩個需要注意的地方為,
(1)在創建MySQL 5.0的配置文件時,port要設置成非3306(如3307等)埠。
(2)將MySQL加入到Windows 的服務中:
打開MS-DOS窗口,進入DOS環境,切換到"%MySQL_HOME%\bin"目錄
運行:
%MySQL_HOME%\bin>mysqld-nt --install mysql5.0
注意:mysqld(或mysqld.exe)是 MySQL server資料庫伺服器的相關程序,而mysqld-nt(或mysqld-nt.exe)是MySQL Daemon資料庫服務的相關程序,在MySQL 5.1(此處的版本為5.1.31)中僅有mysqld(或mysqld.exe),在MySQL 5.0(此處的版本為5.0.77)中,這兩者都有
㈤ android用ZipOutputStream壓縮byte數組怎麼壓縮呀求代碼!在線等,急!!!
OutputStream os = ...
byte[] bytes = ...
GZIPOutputStream zos = new GZIPOutputStream(new BufferedOutputStream(os));
try {
zos.write(bytes);
} finally {
zos.close();
}
android API 文檔中,都有說明
㈥ C# 用7z壓縮一組byte[] 然後對這個byte[]進行解壓。
usingSystem;
usingSystem.IO;
usingSystem.Text;
usingSevenZip;
classProgram
{
staticbyte[]LzmaCompress(byte[]inpbuf)
{
CoderPropID[]propIDs=
{
CoderPropID.DictionarySize,
CoderPropID.PosStateBits,
CoderPropID.LitContextBits,
CoderPropID.LitPosBits,
CoderPropID.Algorithm,
CoderPropID.NumFastBytes,
CoderPropID.MatchFinder,
CoderPropID.EndMarker
};
object[]properties=
{
(Int32)(23),
(Int32)(2),
(Int32)(3),
(Int32)(2),
(Int32)(1),
(Int32)(128),
(string)("bt4"),
(bool)(true)
};
varenc=newSevenZip.Compression.LZMA.Encoder();
enc.SetCoderProperties(propIDs,properties);
MemoryStreammsInp=newMemoryStream(inpbuf);
MemoryStreammsOut=newMemoryStream();
enc.WriteCoderProperties(msOut);
enc.Code(msInp,msOut,-1,-1,null);
returnmsOut.ToArray();
}
staticbyte[]LzmaDecompress(byte[]inpbuf)
{
CoderPropID[]propIDs=
{
CoderPropID.DictionarySize,
CoderPropID.PosStateBits,
CoderPropID.LitContextBits,
CoderPropID.LitPosBits,
CoderPropID.Algorithm,
CoderPropID.NumFastBytes,
CoderPropID.MatchFinder,
CoderPropID.EndMarker
};
object[]properties=
{
(Int32)(23),
(Int32)(2),
(Int32)(3),
(Int32)(2),
(Int32)(1),
(Int32)(128),
(string)("bt4"),
(Int32)(0)
};
vardec=newSevenZip.Compression.LZMA.Decoder();
byte[]prop=newbyte[5];
Array.Copy(inpbuf,prop,5);
dec.SetDecoderProperties(prop);
MemoryStreammsInp=newMemoryStream(inpbuf);
msInp.Seek(5,SeekOrigin.Current);
MemoryStreammsOut=newMemoryStream();
dec.Code(msInp,msOut,-1,-1,null);
returnmsOut.ToArray();
}
staticvoidMain(string[]args)
{
stringtoEnc="kjasdhfkajgj長一點長一點長一點點點點點lrejfoijoigjeoi啊啊啊啊哇哇哇哇啦啦啦asdasfamovijro竟敢浪費看adslf發送端發送端看啦的發散煩";
byte[]pToEnd=Encoding.UTF32.GetBytes(toEnc);
Console.WriteLine("壓縮前{0}個位元組",pToEnd.Length);
byte[]a=LzmaCompress(pToEnd);
Console.WriteLine("壓縮後變為{0}個位元組",a.Length);
byte[]b=LzmaDecompress(a);
stringafterDec=Encoding.UTF32.GetString(b);
if(afterDec==toEnc)
Console.WriteLine("解壓成功");
return;
}
}
DLL是通過它提供的SDK里的代碼編譯來的。lzmasdk下載來後裡面有個文件夾叫cs,就是那裡面的代碼編譯來的。工程里添加DLL引用會吧?
㈦ 用 javax.imageio 實現圖片壓縮原理是什麼
下面是傳進byte[],傳出byte[]數組的,稍微修改即可即可,(data為傳過來的byte[]數組)
Java代碼
ByteArrayInputStream is = new ByteArrayInputStream(data);
BufferedImage src = null;
ByteArrayOutputStream out = null;
ImageWriter imgWrier;
ImageWriteParam imgWriteParams;
// 指定寫圖片的方式為 jpg
imgWrier = ImageIO.getImageWritersByFormatName("jpg").next();
imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam(null);
// 要使用壓縮,必須指定壓縮方式為MODE_EXPLICIT
imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT);
// 這里指定壓縮的程度,參數qality是取值0~1范圍內,
imgWriteParams.setCompressionQuality((float)0.1/data.length);
imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED);
ColorModel colorModel = ColorModel.getRGBdefault();
// 指定壓縮時使用的色彩模式
imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(colorModel, colorModel
.createCompatibleSampleModel(16, 16)));
try {
src = ImageIO.read(is);
out = new ByteArrayOutputStream(data.length);
imgWrier.reset();
// 必須先指定 out值,才能調用write方法, ImageOutputStream可以通過任何 OutputStream構造
imgWrier.setOutput(ImageIO.createImageOutputStream(out));
// 調用write方法,就可以向輸入流寫圖片
imgWrier.write(null, new IIOImage(src, null, null), imgWriteParams);
out.flush();
out.close();
is.close();
data = out.toByteArray();
} catch(Exception e) {
e.printStackTrace();
}
㈧ C#實現 壓縮文件 總是有這句 byte[] bytes = new byte[40960]; 後面的參數傳的什麼意思,
byte[] bytes = new byte[40960];
這種位元組數組通常視為內存塊,做緩存用。後面參數?指40960嗎。
代表這塊內存大約40KB左右
出現「如果傳的參數小 就不能壓縮比 參數大的 文件了?」
說明代碼被寫成先把整個文件讀入緩存。緩存小了,就不能壓。
說明設計寫法錯誤
通常緩存長度不需要太大,使用正確時可以用4K以下緩存壓縮任一長度數據。。
㈨ java編程:數據壓縮規則
你是不是問如何編寫一個程序來壓縮數據?如果你是想解決這個問題的話,繼續往下看,如果你只是想要應付考試的話就算了。
—— 分割線
JDK本身提供了數據壓縮的一個API
java.util.zip.Deflater.deflateBytes(byte[] b, int off, int len)
以下是我使用的一個例子,有點多,注釋看不懂可以問我,不知道怎麼用可以問我,其他的就算了。
/**
* threshold value for compress
*/
public static final int THRESHOLD = 1200;
/**
* Answer a byte array compressed in the DEFLATER format from bytes.
*
* @param bytes
* a byte array
* @return byte[] compressed bytes
* @throws IOException
*/
public static byte[] compress(byte[] bytes)
{
// Create the compressor with highest level of compression
Deflater compressor = new Deflater();
compressor.setLevel(Deflater.BEST_COMPRESSION);
// Give the compressor the data to compress
compressor.setInput(bytes);
compressor.finish();
// Create an expandable byte array to hold the compressed data.
// You cannot use an array that's the same size as the orginal because
// there is no guarantee that the compressed data will be smaller than
// the uncompressed data.
ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
// Compress the data
byte[] buf = new byte[1024];
while (!compressor.finished())
{
int count = compressor.deflate(buf);
bos.write(buf, 0, count);
}
try
{
bos.close();
}
catch (IOException e)
{
}
// Get the compressed data
byte[] compressedData = bos.toByteArray();
return compressedData;
}
/**
* Answer a byte array that has been decompressed from the DEFLATER format.
*
* @param bytes
* a byte array
* @return byte[] compressed bytes
* @throws IOException
*/
public static byte[] uncompress(byte[] bytes)
{
// Create the decompressor and give it the data to compress
Inflater decompressor = new Inflater();
decompressor.setInput(bytes);
// Create an expandable byte array to hold the decompressed data
ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
// Decompress the data
byte[] buf = new byte[1024];
while (!decompressor.finished())
{
try
{
int count = decompressor.inflate(buf);
bos.write(buf, 0, count);
}
catch (DataFormatException e)
{
}
}
try
{
bos.close();
}
catch (IOException e)
{
}
// Get the decompressed data
byte[] decompressedData = bos.toByteArray();
return decompressedData;
}
㈩ 請問C語言中大數組如何處理
你仔細看看這些數據都是有規律的,有相當大的一部分都是0 1 2 3 4 5 6 13重復了很多次。統計一下這些循環出現了多少次,如果考慮這個因素,壓縮還是可以的。
否則的話,所有的數據都是0-15區間的整數,即使考慮使用4位二進制壓縮,1個位元組(8 bit)可以存2個整數,41502個數也需要20K byte,已經超過內存上限了...
如果數據沒有規律,而且實在要壓縮的話,考慮用霍夫曼編碼可能好一些,不過我估計也挺懸的。可以考慮其他方式,不要保存在內存中,程序運行時需要哪一部分數據再讀進來。