refactor: ifaces io png detector
This commit is contained in:
@ -16,46 +16,44 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// you can download the rosie.unknown image in the link:
|
|
||||||
// https://inancgumus.github.com/x/rosie.unknown
|
|
||||||
|
|
||||||
// then feed the file to the standard input of this program:
|
|
||||||
// go run . < rosie.unknown
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// get it from the web server
|
// initiate the transmission channel (http connection) to the webserver.
|
||||||
resp, err := http.Get("https://inancgumus.github.com/x/rosie.unknown")
|
resp, err := http.Get("https://inancgumus.github.com/x/rosie.unknown")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
// close it for keep-alive header.
|
||||||
|
// so the http package can reuse the connection.
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if err := transfer(resp.Body); err != nil {
|
// resp.Body here is an io.ReadCloser: Read() + Close() methods.
|
||||||
|
// but in the transfer function it's an io.Reader: Only the Read() method.
|
||||||
|
n, err := transfer(resp.Body)
|
||||||
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
fmt.Printf("%d bytes transferred.\n", n)
|
||||||
|
|
||||||
|
// resp.Body.Close() runs here (because of the defer above)
|
||||||
}
|
}
|
||||||
|
|
||||||
func transfer(r io.Reader) error {
|
func transfer(r io.Reader) (n int64, err error) {
|
||||||
const pngSign = "\x89PNG\r\n\x1a\n"
|
const pngSign = "\x89PNG\r\n\x1a\n"
|
||||||
const pngSignLen = 8
|
const pngSignLen = 8
|
||||||
|
|
||||||
// create an in-memory buffer (bytes.Buffer is an io.Reader and an io.Writer).
|
// create an in-memory buffer (bytes.Buffer is an io.Reader and an io.Writer).
|
||||||
memory := bytes.Buffer{}
|
var memory bytes.Buffer
|
||||||
|
|
||||||
// stream from the web server to the in-memory buffer in 32KB data chunks.
|
// stream from the standard input to the in-memory buffer in 32KB data chunks.
|
||||||
// resp.Body.Read(...) -> memory.Write(...)
|
// r.Read(...) -> memory.Write(...)
|
||||||
if _, err := io.Copy(&memory, r); err != nil {
|
if n, err = io.Copy(&memory, r); err != nil {
|
||||||
return err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the accumulated bytes from the in-memory buffer.
|
// check the png signature.
|
||||||
buf := memory.Bytes()
|
buf := memory.Bytes()
|
||||||
|
|
||||||
// print the first eight bytes.
|
|
||||||
fmt.Printf("% x\n", buf[:pngSignLen])
|
fmt.Printf("% x\n", buf[:pngSignLen])
|
||||||
|
|
||||||
// compare it with the png signature.
|
|
||||||
fmt.Printf("% x\n", pngSign)
|
fmt.Printf("% x\n", pngSign)
|
||||||
|
|
||||||
return nil
|
return n, err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user