refactor: io png detector ifaces
This commit is contained in:
@ -23,12 +23,6 @@ import (
|
|||||||
// go run . < rosie.unknown
|
// go run . < rosie.unknown
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
const pngSign = "\x89PNG\r\n\x1a\n"
|
|
||||||
const pngSignLen = 8
|
|
||||||
|
|
||||||
// create an in-memory buffer (bytes.Buffer is an io.Reader and an io.Writer).
|
|
||||||
memory := bytes.Buffer{}
|
|
||||||
|
|
||||||
// get it from the web server
|
// get it from the web server
|
||||||
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 {
|
||||||
@ -36,10 +30,22 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if err := transfer(resp.Body); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func transfer(r io.Reader) error {
|
||||||
|
const pngSign = "\x89PNG\r\n\x1a\n"
|
||||||
|
const pngSignLen = 8
|
||||||
|
|
||||||
|
// create an in-memory buffer (bytes.Buffer is an io.Reader and an io.Writer).
|
||||||
|
memory := bytes.Buffer{}
|
||||||
|
|
||||||
// stream from the web server to the in-memory buffer in 32KB data chunks.
|
// stream from the web server to the in-memory buffer in 32KB data chunks.
|
||||||
// resp.Body.Read(...) -> memory.Write(...)
|
// resp.Body.Read(...) -> memory.Write(...)
|
||||||
if _, err := io.Copy(&memory, resp.Body); err != nil {
|
if _, err := io.Copy(&memory, r); err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the accumulated bytes from the in-memory buffer.
|
// get the accumulated bytes from the in-memory buffer.
|
||||||
@ -50,4 +56,6 @@ func main() {
|
|||||||
|
|
||||||
// compare it with the png signature.
|
// compare it with the png signature.
|
||||||
fmt.Printf("% x\n", pngSign)
|
fmt.Printf("% x\n", pngSign)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user