File: IOUtil.java

package info (click to toggle)
mongodb 1%3A2.4.10-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 82,464 kB
  • sloc: cpp: 740,225; ansic: 152,098; sh: 13,820; python: 11,864; makefile: 1,012; perl: 922; pascal: 617; java: 452; lisp: 222; asm: 174
file content (156 lines) | stat: -rw-r--r-- 4,050 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// IOUtil.java

import java.io.*;
import java.net.*;
import java.util.*;

public class IOUtil {

    public static String urlFileName( String url ) {
        int idx = url.lastIndexOf( "/" );
        if ( idx < 0 )
            return url;
        return url.substring( idx + 1 );
    }

    public static long pipe( InputStream in , OutputStream out )
        throws IOException {

        long bytes = 0;

        byte[] buf = new byte[2048];
        
        while ( true ) {
            int x = in.read( buf );
            if ( x < 0 ) 
                break;

            bytes += x;
            out.write( buf , 0 , x );
        }

        return bytes;
    }

    public static class PipingThread extends Thread {
        public PipingThread( InputStream in , OutputStream out ) {
            _in = in;
            _out = out;

            _wrote = 0;
        }
        
        public void run() {
            try {
                _wrote = pipe( _in , _out );
            }
            catch ( IOException ioe ) {
                ioe.printStackTrace();
                _wrote = -1;
            }
        }
        
        public long wrote() {
            return _wrote;
        }

        long _wrote;
        
        final InputStream _in;
        final OutputStream _out;
    }

    public static String readStringFully( InputStream in ) 
        throws IOException {
        
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        pipe( in , bout );
        return new String( bout.toByteArray() , "UTF8" );
        
    }

    public static Map<String,Object> readPythonSettings( File file ) 
        throws IOException {
        
        String all = readStringFully( new FileInputStream( file ) );

        Map<String,Object> map = new TreeMap<String,Object>();
        
        for ( String line : all.split( "\n" ) ) {
            line = line.trim();
            if ( line.length() == 0 )
                continue;
            
            String[] pcs = line.split( "=" );
            if ( pcs.length != 2 )
                continue;
            
            String name = pcs[0].trim();
            String value = pcs[1].trim();
            
            if ( value.startsWith( "\"" ) ) {
                map.put( name , value.substring( 1 , value.length() - 1 ) );
            }
            else {
                map.put( name , Long.parseLong( value ) );
            }
            
        }
        
        return map;
    }

    public static String[] runCommand( String cmd , File dir )
        throws IOException {
        
        Process p = Runtime.getRuntime().exec( cmd.split( " +" ) , new String[]{} , dir );
        String[] results = new String[]{ IOUtil.readStringFully( p.getInputStream() ) , IOUtil.readStringFully( p.getErrorStream() ) };
        try {
            if ( p.waitFor() != 0 )
                throw new RuntimeException( "command failed [" + cmd + "]\n" + results[0] + "\n" + results[1] );
        }
        catch ( InterruptedException ie ) {
            throw new RuntimeException( "uh oh" );
        }
        return results;
    }


    public static void download( String http , File localDir ) 
        throws IOException {
        
        File f = localDir;
        f.mkdirs();
        
        f = new File( f.toString() + File.separator + urlFileName( http ) );
        
        System.out.println( "downloading\n\t" + http + "\n\t" + f );
        
        if ( f.exists() ) {
            System.out.println( "\t already exists" );
            return;
        }
        
        URL url = new URL( http );
        
        InputStream in = url.openConnection().getInputStream();
        OutputStream out = new FileOutputStream( f );
        
        pipe( in , out );
        
        out.close();
        in.close();
        
    }

    public static void main( String[] args )
        throws Exception {
        
        
        byte[] data = new byte[]{ 'e' , 'r' , 'h' , 0 };
        System.out.write( data );
        System.out.println( "yo" );
        
    }

}