2011-01-15 07:34:30 +08:00
|
|
|
package iconv
|
|
|
|
|
2012-04-11 06:30:42 +08:00
|
|
|
import (
|
2011-01-15 07:34:30 +08:00
|
|
|
"io"
|
2012-04-11 06:30:42 +08:00
|
|
|
"syscall"
|
2011-01-15 07:34:30 +08:00
|
|
|
)
|
|
|
|
|
2016-06-18 17:02:05 +08:00
|
|
|
const bufferSize = 8 * 1024
|
|
|
|
|
2011-01-15 07:34:30 +08:00
|
|
|
type Reader struct {
|
2012-04-11 06:30:42 +08:00
|
|
|
source io.Reader
|
|
|
|
converter *Converter
|
|
|
|
buffer []byte
|
2011-01-15 17:06:50 +08:00
|
|
|
readPos, writePos int
|
2012-04-11 06:30:42 +08:00
|
|
|
err error
|
2011-01-15 07:34:30 +08:00
|
|
|
}
|
|
|
|
|
2012-04-11 06:30:42 +08:00
|
|
|
func NewReader(source io.Reader, fromEncoding string, toEncoding string) (*Reader, error) {
|
2011-01-15 07:34:30 +08:00
|
|
|
// create a converter
|
|
|
|
converter, err := NewConverter(fromEncoding, toEncoding)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return NewReaderFromConverter(source, converter), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the error
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewReaderFromConverter(source io.Reader, converter *Converter) (reader *Reader) {
|
|
|
|
reader = new(Reader)
|
|
|
|
|
|
|
|
// copy elements
|
|
|
|
reader.source = source
|
|
|
|
reader.converter = converter
|
|
|
|
|
|
|
|
// create 8K buffers
|
2016-06-18 17:02:05 +08:00
|
|
|
reader.buffer = make([]byte, bufferSize)
|
2011-01-15 07:34:30 +08:00
|
|
|
|
|
|
|
return reader
|
|
|
|
}
|
|
|
|
|
2016-06-18 17:02:05 +08:00
|
|
|
func (this *Reader) fillBuffer() int {
|
2011-01-15 07:34:30 +08:00
|
|
|
// slide existing data to beginning
|
2011-01-15 17:06:50 +08:00
|
|
|
if this.readPos > 0 {
|
|
|
|
// copy current bytes - is this guaranteed safe?
|
|
|
|
copy(this.buffer, this.buffer[this.readPos:this.writePos])
|
2011-01-15 07:34:30 +08:00
|
|
|
|
|
|
|
// adjust positions
|
2011-01-15 17:06:50 +08:00
|
|
|
this.writePos -= this.readPos
|
|
|
|
this.readPos = 0
|
2011-01-15 07:34:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// read new data into buffer at write position
|
2011-01-15 17:06:50 +08:00
|
|
|
bytesRead, err := this.source.Read(this.buffer[this.writePos:])
|
2011-01-15 07:34:30 +08:00
|
|
|
|
|
|
|
// adjust write position
|
2011-01-15 17:06:50 +08:00
|
|
|
this.writePos += bytesRead
|
2011-01-15 07:34:30 +08:00
|
|
|
|
2011-01-15 17:06:50 +08:00
|
|
|
// track any reader error / EOF
|
2011-01-15 07:34:30 +08:00
|
|
|
if err != nil {
|
2011-01-15 17:06:50 +08:00
|
|
|
this.err = err
|
2011-01-15 07:34:30 +08:00
|
|
|
}
|
2016-06-23 10:56:59 +08:00
|
|
|
return bytesRead
|
2011-01-15 07:34:30 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 17:06:50 +08:00
|
|
|
// implement the io.Reader interface
|
2012-04-11 06:30:42 +08:00
|
|
|
func (this *Reader) Read(p []byte) (n int, err error) {
|
2011-01-15 17:06:50 +08:00
|
|
|
// checks for when we have no data
|
|
|
|
for this.writePos == 0 || this.readPos == this.writePos {
|
|
|
|
// if we have an error / EOF, just return it
|
|
|
|
if this.err != nil {
|
|
|
|
return n, this.err
|
|
|
|
}
|
2011-01-15 07:34:30 +08:00
|
|
|
|
2011-01-15 17:06:50 +08:00
|
|
|
// else, fill our buffer
|
|
|
|
this.fillBuffer()
|
2012-04-11 06:30:42 +08:00
|
|
|
}
|
2011-01-15 07:34:30 +08:00
|
|
|
|
2011-01-15 17:06:50 +08:00
|
|
|
// TODO: checks for when we have less data than len(p)
|
2011-01-15 07:34:30 +08:00
|
|
|
|
2011-01-15 17:06:50 +08:00
|
|
|
// we should have an appropriate amount of data, convert it into the given buffer
|
|
|
|
bytesRead, bytesWritten, err := this.converter.Convert(this.buffer[this.readPos:this.writePos], p)
|
2011-01-15 07:34:30 +08:00
|
|
|
|
2011-01-15 17:06:50 +08:00
|
|
|
// adjust byte counters
|
|
|
|
this.readPos += bytesRead
|
|
|
|
n += bytesWritten
|
2011-01-15 07:34:30 +08:00
|
|
|
|
2011-01-15 17:06:50 +08:00
|
|
|
// if we experienced an iconv error, check it
|
|
|
|
if err != nil {
|
2016-06-18 17:02:05 +08:00
|
|
|
// EINVAL:
|
|
|
|
// An incomplete multibyte sequence is encountered in the input,
|
|
|
|
// and the input byte sequence terminates after it.
|
|
|
|
if err == syscall.EINVAL {
|
|
|
|
// If we can read new data, then this should NOT be
|
|
|
|
// considered as an error.
|
|
|
|
newData := this.fillBuffer()
|
|
|
|
if newData > 0 {
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-15 17:06:50 +08:00
|
|
|
// E2BIG errors can be ignored (we'll get them often) as long
|
|
|
|
// as at least 1 byte was written. If we experienced an E2BIG
|
|
|
|
// and no bytes were written then the buffer is too small for
|
|
|
|
// even the next character
|
2012-04-11 06:30:42 +08:00
|
|
|
if err != syscall.E2BIG || bytesWritten == 0 {
|
2011-01-15 17:06:50 +08:00
|
|
|
// track anything else
|
|
|
|
this.err = err
|
|
|
|
}
|
2011-01-15 07:34:30 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 17:06:50 +08:00
|
|
|
// return our results
|
|
|
|
return n, this.err
|
2011-01-15 07:34:30 +08:00
|
|
|
}
|