forked from shawnfeng/joy4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
547 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package bits | ||
|
||
import ( | ||
"io" | ||
) | ||
|
||
type Reader struct { | ||
R io.Reader | ||
n int | ||
bits uint64 | ||
} | ||
|
||
func (self *Reader) ReadBits64(n int) (bits uint64, err error) { | ||
if self.n < n { | ||
var b [8]byte | ||
var got int | ||
want := (n - self.n + 7) / 8 | ||
if got, err = self.R.Read(b[:want]); err != nil { | ||
return | ||
} | ||
if got < want { | ||
err = io.EOF | ||
return | ||
} | ||
for i := 0; i < got; i++ { | ||
self.bits <<= 8 | ||
self.bits |= uint64(b[i]) | ||
} | ||
self.n += got * 8 | ||
} | ||
bits = self.bits >> uint(self.n-n) | ||
self.bits ^= bits << uint(self.n-n) | ||
self.n -= n | ||
return | ||
} | ||
|
||
func (self *Reader) ReadBits(n int) (bits uint, err error) { | ||
var bits64 uint64 | ||
if bits64, err = self.ReadBits64(n); err != nil { | ||
return | ||
} | ||
bits = uint(bits64) | ||
return | ||
} | ||
|
||
func (self *Reader) Read(p []byte) (n int, err error) { | ||
for n < len(p) { | ||
want := 8 | ||
if len(p)-n < want { | ||
want = len(p) - n | ||
} | ||
var bits uint64 | ||
if bits, err = self.ReadBits64(want * 8); err != nil { | ||
break | ||
} | ||
for i := 0; i < want; i++ { | ||
p[n+i] = byte(bits >> uint((want-i-1)*8)) | ||
} | ||
n += want | ||
} | ||
return | ||
} | ||
|
||
type Writer struct { | ||
W io.Writer | ||
n int | ||
bits uint64 | ||
} | ||
|
||
func (self *Writer) WriteBits64(bits uint64, n int) (err error) { | ||
if self.n+n > 64 { | ||
move := uint(64 - self.n) | ||
mask := bits >> move | ||
self.bits = (self.bits << move) | mask | ||
self.n = 64 | ||
if err = self.FlushBits(); err != nil { | ||
return | ||
} | ||
n -= int(move) | ||
bits ^= (mask << move) | ||
} | ||
self.bits = (self.bits << uint(n)) | bits | ||
self.n += n | ||
return | ||
} | ||
|
||
func (self *Writer) WriteBits(bits uint, n int) (err error) { | ||
return self.WriteBits64(uint64(bits), n) | ||
} | ||
|
||
func (self *Writer) Write(p []byte) (n int, err error) { | ||
for n < len(p) { | ||
if err = self.WriteBits64(uint64(p[n]), 8); err != nil { | ||
return | ||
} | ||
n++ | ||
} | ||
return | ||
} | ||
|
||
func (self *Writer) FlushBits() (err error) { | ||
if self.n > 0 { | ||
var b [8]byte | ||
bits := self.bits | ||
if self.n%8 != 0 { | ||
bits <<= uint(8 - (self.n % 8)) | ||
} | ||
want := (self.n + 7) / 8 | ||
for i := 0; i < want; i++ { | ||
b[i] = byte(bits >> uint((want-i-1)*8)) | ||
} | ||
if _, err = self.W.Write(b[:want]); err != nil { | ||
return | ||
} | ||
self.n = 0 | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package bits | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestBits(t *testing.T) { | ||
rdata := []byte{0xf3, 0xb3, 0x45, 0x60} | ||
rbuf := bytes.NewReader(rdata[:]) | ||
r := &Reader{R: rbuf} | ||
var u32 uint | ||
if u32, _ = r.ReadBits(4); u32 != 0xf { | ||
t.FailNow() | ||
} | ||
if u32, _ = r.ReadBits(4); u32 != 0x3 { | ||
t.FailNow() | ||
} | ||
if u32, _ = r.ReadBits(2); u32 != 0x2 { | ||
t.FailNow() | ||
} | ||
if u32, _ = r.ReadBits(2); u32 != 0x3 { | ||
t.FailNow() | ||
} | ||
b := make([]byte, 2) | ||
if r.Read(b); b[0] != 0x34 || b[1] != 0x56 { | ||
t.FailNow() | ||
} | ||
|
||
wbuf := &bytes.Buffer{} | ||
w := &Writer{W: wbuf} | ||
w.WriteBits(0xf, 4) | ||
w.WriteBits(0x3, 4) | ||
w.WriteBits(0x2, 2) | ||
w.WriteBits(0x3, 2) | ||
n, _ := w.Write([]byte{0x34, 0x56}) | ||
if n != 2 { | ||
t.FailNow() | ||
} | ||
w.FlushBits() | ||
wdata := wbuf.Bytes() | ||
if wdata[0] != 0xf3 || wdata[1] != 0xb3 || wdata[2] != 0x45 || wdata[3] != 0x60 { | ||
t.FailNow() | ||
} | ||
|
||
b = make([]byte, 8) | ||
PutUInt64BE(b, 0x11223344, 32) | ||
if b[0] != 0x11 || b[1] != 0x22 || b[2] != 0x33 || b[3] != 0x44 { | ||
t.FailNow() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package bufio | ||
|
||
import ( | ||
"io" | ||
) | ||
|
||
type Reader struct { | ||
buf [][]byte | ||
R io.ReadSeeker | ||
} | ||
|
||
func NewReaderSize(r io.ReadSeeker, size int) *Reader { | ||
buf := make([]byte, size*2) | ||
return &Reader{ | ||
R: r, | ||
buf: [][]byte{buf[0:size], buf[size:]}, | ||
} | ||
} | ||
|
||
func (self *Reader) ReadAt(b []byte, off int64) (n int, err error) { | ||
return | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package bits | ||
|
||
import ( | ||
"io" | ||
) | ||
|
||
type GolombBitReader struct { | ||
R io.Reader | ||
buf [1]byte | ||
left byte | ||
} | ||
|
||
func (self *GolombBitReader) ReadBit() (res uint, err error) { | ||
if self.left == 0 { | ||
if _, err = self.R.Read(self.buf[:]); err != nil { | ||
return | ||
} | ||
self.left = 8 | ||
} | ||
self.left-- | ||
res = uint(self.buf[0]>>self.left) & 1 | ||
return | ||
} | ||
|
||
func (self *GolombBitReader) ReadBits(n int) (res uint, err error) { | ||
for i := 0; i < n; i++ { | ||
var bit uint | ||
if bit, err = self.ReadBit(); err != nil { | ||
return | ||
} | ||
res |= bit << uint(n-i-1) | ||
} | ||
return | ||
} | ||
|
||
func (self *GolombBitReader) ReadExponentialGolombCode() (res uint, err error) { | ||
i := 0 | ||
for { | ||
var bit uint | ||
if bit, err = self.ReadBit(); err != nil { | ||
return | ||
} | ||
if !(bit == 0 && i < 32) { | ||
break | ||
} | ||
i++ | ||
} | ||
if res, err = self.ReadBits(i); err != nil { | ||
return | ||
} | ||
res += (1 << uint(i)) - 1 | ||
return | ||
} | ||
|
||
func (self *GolombBitReader) ReadSE() (res uint, err error) { | ||
if res, err = self.ReadExponentialGolombCode(); err != nil { | ||
return | ||
} | ||
if res&0x01 != 0 { | ||
res = (res + 1) / 2 | ||
} else { | ||
res = -res / 2 | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
package pio | ||
|
||
var RecommendBufioSize = 1024*64 | ||
|
Oops, something went wrong.