How do I save a Net::BER::BerIdentifiedString as a binary file?

Anyone know how to save a Net::BER::BerIdentifiedString as a binary
file?

I’ve tried .dump, but that did not work. I noticed there’s a .bytes
enumerator, but I’m not sure what to do with it.

Basically, I have an Net::BER::BerIdentifiedString [Binary Data] which
is holding a profile photo from an LDAP call, and I want to save it as a
jpg file.

On Fri, Feb 10, 2012 at 11:18 PM, Edward S. [email protected]
wrote:

Anyone know how to save a Net::BER::BerIdentifiedString as a binary
file?

I’ve tried .dump, but that did not work. I noticed there’s a .bytes
enumerator, but I’m not sure what to do with it.

Basically, I have an Net::BER::BerIdentifiedString [Binary Data] which
is holding a profile photo from an LDAP call, and I want to save it as a
jpg file.

File.open ‘foo.jpg’, ‘wb’ do |io|
io.write bytes
end

Something like that.

Kind regards

robert

Robert K. wrote in post #1046419:

On Fri, Feb 10, 2012 at 11:18 PM, Edward S. [email protected]
wrote:

Anyone know how to save a Net::BER::BerIdentifiedString as a binary
file?

I’ve tried .dump, but that did not work. I noticed there’s a .bytes
enumerator, but I’m not sure what to do with it.

Basically, I have an Net::BER::BerIdentifiedString [Binary Data] which
is holding a profile photo from an LDAP call, and I want to save it as a
jpg file.

File.open ‘foo.jpg’, ‘wb’ do |io|
io.write bytes
end

Something like that.

Kind regards

robert

That does not work. It results in foo.jpg with the following content:

#Enumerator:0x7336ae8

Edward S. wrote in post #1045292:

Anyone know how to save a Net::BER::BerIdentifiedString as a binary
file?

I’ve tried .dump, but that did not work. I noticed there’s a .bytes
enumerator, but I’m not sure what to do with it.

Basically, I have an Net::BER::BerIdentifiedString [Binary Data] which
is holding a profile photo from an LDAP call, and I want to save it as a
jpg file.

Never mind. I got this working by just writing the BerIndentified
String; not sure why it didn’t work when I first tried it in Rails.

Anyway, here’s the snippet:

ldap.search(:base => base, :filter => filter, :return_result => true)
do |entry|
[:thumbnailphoto, :jpegphoto, :photo].each do |photo_key|
if entry.attribute_names.include?(photo_key)
@ldap_photo = entry[photo_key][0]
File.open(file_name, ‘wb’) { |f| f.write(@ldap_photo) }
break
end
end
end

Net::BER::BerIdentifiedString is just a string with a ber indentifier
attached.
Source: http://net-ldap.rubyforge.org/Net/BER/BerIdentifiedString.html

(no matter what stands for ‘ber’).

So, you should know how the .jpg is being encoded inside the String!
Strings are not just a sequence of bytes (See character encoding)
Is it Base64 encoded?

You can try to Marshal.dump the object and see if somebody can guess.

File.open(“ber_jpeg_file.dump”, ‘w’) {|f| f.write
Marshal.dump(your_ber_object) }

Abinoam Jr.

On Mon, Feb 13, 2012 at 6:23 AM, Robert K.