99 "github.com/spf13/pflag"
1010)
1111
12- func hexdump (fileName string , offset , length int64 ) error {
12+ func hexdump (fileName string , offset , length int64 , w io. Writer ) error {
1313
1414 // Open the file
1515 file , err := os .Open (fileName )
@@ -24,7 +24,7 @@ func hexdump(fileName string, offset, length int64) error {
2424 }
2525 }
2626
27- reader := bufio .NewReader (file )
27+ reader := bufio .NewReaderSize (file , 1024 * 1024 )
2828
2929 var count int64 = 0
3030 for {
@@ -38,7 +38,7 @@ func hexdump(fileName string, offset, length int64) error {
3838 return err
3939 }
4040
41- printLine (offset + count , buf [:n ])
41+ printLine (w , offset + count , buf [:n ])
4242
4343 count += int64 (n )
4444 if length > 0 && count >= length {
@@ -48,39 +48,39 @@ func hexdump(fileName string, offset, length int64) error {
4848 return nil
4949}
5050
51- func printLine (offset int64 , buf []byte ) {
51+ func printLine (w io. Writer , offset int64 , buf []byte ) {
5252 // Print the offset
53- fmt .Printf ( "%08x " , offset )
53+ fmt .Fprintf ( w , "%08x " , offset )
5454
5555 // Print the hex values
5656 for i := 0 ; i < len (buf ); i ++ {
5757 if i % 8 == 0 && i != 0 {
58- fmt .Print ( " " )
58+ fmt .Fprint ( w , " " )
5959 }
60- fmt .Printf ( "%02x " , buf [i ])
60+ fmt .Fprintf ( w , "%02x " , buf [i ])
6161 }
6262 // Pad the line to 48 characters
6363 for i := len (buf ); i < 16 ; i ++ {
6464 if i % 8 == 0 && i != 0 {
65- fmt .Print ( " " )
65+ fmt .Fprintf ( w , " " )
6666 }
67- fmt .Print ( " " )
67+ fmt .Fprint ( w , " " )
6868 }
6969
7070 // Print the ASCII values
71- fmt .Print ( " |" )
71+ fmt .Fprint ( w , " |" )
7272 for _ , b := range buf {
7373 if b >= 32 && b <= 126 {
74- fmt .Printf ( "%c" , b )
74+ fmt .Fprintf ( w , "%c" , b )
7575 } else {
76- fmt .Print ( "." )
76+ fmt .Fprint ( w , "." )
7777 }
7878 }
7979 // Pad the line to 16 characters
8080 for i := len (buf ); i < 16 ; i ++ {
81- fmt .Print ( " " )
81+ fmt .Fprint ( w , " " )
8282 }
83- fmt .Println ( "|" )
83+ fmt .Fprintln ( w , "|" )
8484}
8585
8686func main () {
@@ -106,7 +106,7 @@ func main() {
106106 arg = "/dev/stdin"
107107 }
108108
109- if err := hexdump (arg , * offset , * length ); err != nil {
109+ if err := hexdump (arg , * offset , * length , bufio . NewWriter ( os . Stdout ) ); err != nil {
110110 panic (err )
111111 }
112112 }
0 commit comments