Module: PkernelJce::JFile
- Defined in:
- lib/pkernel_jce/jfile.rb
Class Method Summary collapse
-
.absolute_path(file) ⇒ Object
create_temp_file.
-
.copy(src, dest, opts = { }, &block) ⇒ Object
equals?.
- .create_temp_file(opts = { }) ⇒ Object
-
.equal?(*files, &block) ⇒ Boolean
absolute_path.
Instance Method Summary collapse
Class Method Details
.absolute_path(file) ⇒ Object
create_temp_file
12 13 14 15 16 17 18 |
# File 'lib/pkernel_jce/jfile.rb', line 12 def JFile.absolute_path(file) if file.nil? raise PkernelJce::Error, "Cannot get absolute path for file is nil" else file.absolute_path end end |
.copy(src, dest, opts = { }, &block) ⇒ Object
equals?
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 |
# File 'lib/pkernel_jce/jfile.rb', line 61 def JFile.copy(src,dest,opts = { },&block) raise PkernelJce::Error, "Source cannot be nil or empty" if src.nil? or src.empty? raise PkernelJce::Error, "Destination cannot be nil" if dest.nil? if src.is_a?(java.io.InputStream) src = src srcLength = src.length elsif src.is_a?(String) # assume is file path? f = java.io.File.new(src) raise PkernelJce::Error, "Given file doesn't exist [#{f.absolute_path}]" if not f.exists raise PkernelJce::Error, "Cannot read source file from a directory [#{f.absolute_path}]" if f.is_directory? src = java.io.FileInputStream.new(f) srcLength = f.length else raise PkernelJce::Error, "Unsupported source file type #{src.class}" end if dest.is_a?(java.io.OutputStream) PkernelJce::GConf.instance.glog.debug "Given dest is an OutputStream. Direct use the object." os = dest elsif dest.is_a?(String) # assume is file path? f = java.io.File.new(dest) raise PkernelJce::Error, "Cannot write dest to a directory [#{f.absolute_path}]" if f.is_directory? PkernelJce::GConf.instance.glog.debug "Opening given path #{f.absolute_path} to copy file into" os = java.io.FileOutputStream.new(f) else raise PkernelJce::Error, "Unsupported destination type #{dest.class}" end bufSize = opts[:bufSize] || 1024000 b = Java::byte[bufSize].new totalWritten = 0 while((read = src.read(b,0,b.length)) != -1) if block block.call(:before_write, { buf: b, from: 0, len: read }) end os.write(b,0,read) totalWritten += read if block block.call(:progress, { total: srcLength, written: read, processed: totalWritten }) end end end |
.create_temp_file(opts = { }) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/pkernel_jce/jfile.rb', line 5 def JFile.create_temp_file(opts = { }) prefix = opts[:prefix] suffix = opts[:suffix] dir = opts[:directory] java.io.File.createTempFile(prefix,suffix,dir) end |
.equal?(*files, &block) ⇒ Boolean
absolute_path
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 |
# File 'lib/pkernel_jce/jfile.rb', line 20 def JFile.equal?(*files,&block) if files.length > 1 if block bufLen = block.call(:bufLen) end bufLen = bufLen || 102400 equal = false result = nil md = java.security.MessageDigest.getInstance("SHA256") files.each do |f| ff = java.io.File.new(f) if ff.exist? buf = Java::byte[bufLen].new fis = java.io.FileInputStream.new(ff) while((read = fis.read(buf,0,buf.length)) != -1) md.update(buf,0,read) end res = md.digest if result.nil? result = res elsif not java.util.Arrays.is_equals(res, result) equal = false break else result = res end md.reset else raise PkernelJce::Error, "Given file to compare equal '#{f}' does not exist" end end equal else true end end |
Instance Method Details
#to_memory(file, opts = { }, &block) ⇒ Object
copy
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/pkernel_jce/jfile.rb', line 112 def to_memory(file, opts = { }, &block) raise PkernelJce::Error, "No file given" if file.nil? f = java.io.File.new(file) raise PkernelJce::Error, "Given file #{f.absolute_path} does not exist" if not f.exists raise PkernelJce::Error, "Given file #{f.absolute_path} is a directory" if f.directory? total = f.length bufSize = opts[:bufSize] || 1024000 b = Java::byte[bufSize].new baos = java.io.ByteArrayOutputStream.new processed = 0 fis = java.io.FileInputStream.new(f) while((read = fis.read(b,0,b.length)) != -1) baos.write(b,0,read) processed += read block.call(:progress, { total: total, processed: processed, wwritten: read }) if block end baos.toByteArray end |