June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,84 @@
// version 1.2.31
import java.io.File
class BitFilter(val f: File, var accu: Int = 0, var bits: Int = 0) {
private val bw = f.bufferedWriter()
private val br = f.bufferedReader()
fun write(buf: ByteArray, start: Int, _nBits: Int, _shift: Int) {
var nBits = _nBits
var index = start + _shift / 8
var shift = _shift % 8
while (nBits != 0 || bits >= 8) {
while (bits >= 8) {
bits -= 8
bw.write(accu ushr bits)
accu = accu and ((1 shl bits) - 1)
}
while (bits < 8 && nBits != 0) {
val b = buf[index].toInt()
accu = (accu shl 1) or (((128 ushr shift) and b) ushr (7 - shift))
nBits--
bits++
if (++shift == 8) { shift = 0; index++ }
}
}
}
fun read(buf: ByteArray, start: Int, _nBits: Int, _shift: Int) {
var nBits = _nBits
var index = start + _shift / 8
var shift = _shift % 8
while (nBits != 0) {
while (bits != 0 && nBits != 0) {
val mask = 128 ushr shift
if ((accu and (1 shl (bits - 1))) != 0)
buf[index] = (buf[index].toInt() or mask).toByte()
else
buf[index] = (buf[index].toInt() and mask.inv()).toByte()
nBits--
bits--
if (++shift >= 8) { shift = 0; index++ }
}
if (nBits == 0) break
accu = (accu shl 8) or br.read()
bits += 8
}
}
fun closeWriter() {
if (bits != 0) {
accu = (accu shl (8 - bits))
bw.write(accu)
}
bw.close()
accu = 0
bits = 0
}
fun closeReader() {
br.close()
accu = 0
bits = 0
}
}
fun main(args: Array<String>) {
val s = "abcdefghijk".toByteArray(Charsets.UTF_8)
val f = File("test.bin")
val bf = BitFilter(f)
/* for each byte in s, write 7 bits skipping 1 */
for (i in 0 until s.size) bf.write(s, i, 7, 1)
bf.closeWriter()
/* read 7 bits and expand to each byte of s2 skipping 1 bit */
val s2 = ByteArray(s.size)
for (i in 0 until s2.size) bf.read(s2, i, 7, 1)
bf.closeReader()
println(String(s2, Charsets.UTF_8))
}

View file

@ -4,10 +4,16 @@ class BitWriter(object):
self.bcount = 0
self.out = f
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.flush()
def __del__(self):
try:
self.flush()
except ValueError: # I/O operation on closed file
except ValueError: # I/O operation on closed file.
pass
def _writebit(self, bit):
@ -35,6 +41,12 @@ class BitReader(object):
self.bcount = 0
self.read = 0
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
pass
def _readbit(self):
if not self.bcount:
a = self.input.read(1)
@ -56,22 +68,22 @@ class BitReader(object):
if __name__ == '__main__':
import os
import sys
# determine module name from this file's name and import it
# Determine this module's name from it's file name and import it.
module_name = os.path.splitext(os.path.basename(__file__))[0]
bitio = __import__(module_name)
with open('bitio_test.dat', 'wb') as outfile:
writer = bitio.BitWriter(outfile)
chars = '12345abcde'
for ch in chars:
writer.writebits(ord(ch), 7)
with bitio.BitWriter(outfile) as writer:
chars = '12345abcde'
for ch in chars:
writer.writebits(ord(ch), 7)
with open('bitio_test.dat', 'rb') as infile:
reader = bitio.BitReader(infile)
chars = []
while True:
x = reader.readbits(7)
if reader.read == 0:
break
chars.append(chr(x))
print(''.join(chars))
with bitio.BitReader(infile) as reader:
chars = []
while True:
x = reader.readbits(7)
if not reader.read: # End-of-file?
break
chars.append(chr(x))
print(''.join(chars))

View file

@ -6,3 +6,4 @@ c = sys.stdin.read(1)
while len(c) > 0:
o.writebits(ord(c), 7)
c = sys.stdin.read(1)
o.flush()