Hi all I''m creating a Ruby client for some web service and need to send a file to that service. Since I''m a newbie, I have problems translating this Java code into Ruby: byte[] origFile = FileUtils.readFileToByteArray(new File(filePath)); String content = encodeBase64(new String(origFile, "ISO-8859-1"))); So, I need to get a file as an array of bytes and then transform it into ISO-8859-1 encoded string that should be Base64 encoded afterwards. I tried the following: origFile = "" open(filePath) { |f| origFile += f.gets} content = Base64.encode64(origFile) hoping that, eventually, everything will somehow work ok :) Of course it didn''t... (the contents were similar, but not the same) Help! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 30, 2007, at 8:02 AM, mare wrote:> > Hi all > > I''m creating a Ruby client for some web service and need to send a > file > to that service. Since I''m a newbie, I have problems translating this > Java code into Ruby: > > byte[] origFile = FileUtils.readFileToByteArray(new File(filePath)); > String content = encodeBase64(new String(origFile, "ISO-8859-1"))); > > So, I need to get a file as an array of bytes and then transform it > into > ISO-8859-1 encoded string that should be Base64 encoded afterwards. > > I tried the following: > > origFile = "" > open(filePath) { |f| origFile += f.gets} > content = Base64.encode64(origFile) > > hoping that, eventually, everything will somehow work ok :) > Of course it didn''t... (the contents were similar, but not the same) > > Help!You''re close, try this: content = Base64.encode64(File.read(filePath)) -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn wrote:> On Mar 30, 2007, at 8:02 AM, mare wrote: > > You''re close, try this: > > content = Base64.encode64(File.read(filePath)) >tried, but the results from Java and this code do not match.. Any ideas why? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Apr 2, 2007, at 5:41 AM, Marko Marjanovic wrote:> Rob Biedenharn wrote: >> On Mar 30, 2007, at 8:02 AM, mare wrote: >> >> You''re close, try this: >> >> content = Base64.encode64(File.read(filePath)) > > tried, but the results from Java and this code do not match.. Any > ideas > why?If you''re on Windoze, perhaps: f = File.open(filePath, ''rb'') encoded = f.read f.close content = Base64.encode64(encoded) Or take the Base64 out of the picture and make sure that you get the same result on a plain file. If you have a difference in the length of the output by 4/3 of the number of lines in the original, it''s very likely a line ending difference. Other than that, no one is going to be able to help without more information. Perhaps create a small file and post the result if you can''t figure it out from there. $ irb >> require ''base64'' => true >> filename = "sample.txt" => "sample.txt" >> File.open(filename, "wb") { |f| f.write "hello, world\n" } => 13 >> puts Base64.encode64(File.read(filename)) aGVsbG8sIHdvcmxkCg==> nil >> puts Base64.decode64("aGVsbG8sIHdvcmxkCg==") hello, world => nil >> p Base64.decode64("aGVsbG8sIHdvcmxkCg==") "hello, world\n" => nil -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> If you''re on Windoze, perhaps: > f = File.open(filePath, ''rb'') > encoded = f.read > f.close > content = Base64.encode64(encoded)I tried the code you suggested and compared the results with the Java''s output. The following happened: 1) Performed on a small csv file, both Java and Ruby gave the same output (the file was cp1252 encoded with \r\n line endings). 2) Performed on the same file saved as xls (Excel) spreadsheet, Java gave cp1252 encoded output with \r\n line endings and Ruby gave utf-8 encoded output with \n line endings. I read these attributes from JEdit. Both Java and Ruby codes were saved and run from Eclipse. Any ideas how it could be fixed? Should I provide more (and what) information? Thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Anyone? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---