An iterable array

Java has the nice Iterable interface (since Java 5, i guess) that allows object oriented loops like

List<String> strings = new ArrayList<String>();
for(String string : strings) 
	System.out.println(string);

but guess what, a simple array is not iterable…

In case you need one, feel free to use this one:

package ac.simons;
 
import java.util.Iterator;
 
public class IterableArray<T> implements Iterable<T> {
	private class ArrayIteratorImpl implements Iterator<T> {
		private int position = 0;
		@Override
		public boolean hasNext() {
			return data != null && this.position < data.length;
		}
 
		@Override
		public T next() {
			return data[position++];
		}
 
		@Override
		public void remove() {
			throw new UnsupportedOperationException();
		}		
	}
 
	private final T[] data;
 
	public IterableArray(T[] data) {
		this.data = data;
	}
 
	@Override
	public Iterator<T> iterator() {
		return new ArrayIteratorImpl();
	}	
}

| Comments (0) »

12-Jan-10


Create ZIP Archives containing Unicode filenames with Java

It is harder than i thought to create a simple Zip Archive from within Java that contains entries with unicode names in it.

I’m actually to lazy to read all the specs, but it says something that the entries in a zip archive are encoded using “Cp437”. The buildin Java compressing api has nothing to offer for setting the encoding so i tried Apache Commons Compress.

The manual says the following about interop :

For maximum interop it is probably best to set the encoding to UTF-8, enable the language encoding flag and create Unicode extra fields when writing ZIPs. Such archives should be extracted correctly by java.util.zip, 7Zip, WinZIP, PKWARE tools and most likely InfoZIP tools. They will be unusable with Windows’ “compressed folders” feature and bigger than archives without the Unicode extra fields, though.

That didn’t work for me.

After some cursing, this is my solution:

final ZipArchiveOutputStream zout = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(fc.getSelectedFile())));
zout.setEncoding("Cp437");
zout.setFallbackToUTF8(true);
zout.setUseLanguageEncodingFlag(true);								
zout.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.NOT_ENCODEABLE);

I specifying explicitly the encoding but instead of using utf-8, which didn’t work for my utf-8 strings (wtf??), i’m using the Cp437 from the specs and some other magic options and it works for me in 7zip, WinZip and even Windows’ “compressed folders”.

Edit: Unfortunately, in Mac OS X’s Unzip utility, the non Cp437 are broken. If anyone has a good idea, feel free to leave a comment.

| Comments (15) »

05-Jan-10


Today: Fun with Unicode, Regex and Java.

Some would say, i have 3 problems 😉

private final static Pattern placeholder = Pattern.compile("#\\{(\\w+?)\\}");

won’t match “Mot#{ö}rhead” for example.

To replace the word character \w you either need the list of possible unicodeblocks like [\p{InLatin}|\p{InEtc}] (you get the codes for the blocks through “Character.UnicodeBlock.forName” or you’re lazy like me and just use the dot:

private final static Pattern placeholder = Pattern.compile("#\\{(.+?)\\}");

Oh what a day… :/

| Comments (0) »

03-Nov-09


How to change the image assets path in Rails

I found no other way to change the path of “images” in a Rails application than monkey patching the AssetTagHelper like so:

module ActionView
  module Helpers #:nodoc:
    module AssetTagHelper
      def image_path(source)
        compute_public_path(source, 'static_images')
      end
      alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
    end
  end
end

| Comments (0) »

01-Nov-09


Redcloth / Textilize :hard_breaks is broken in Rails 2.3.4

The default behavior for quite a long time was :hard_breaks in the textilize helper method in rails.

Hard Breaks means: All line breaks are turned into <br />’s.

Somebody changed the textilize helper in “actionpack-2.3.4/lib/action_view/helpers/text_helper.rb” in 2.3.4 and added the ability to pass some options but broke the default behavior here:

def textilize(text, *options)
        options ||= [:hard_breaks]
        # ...

Options will never be null.

I fixed this by monkey patching the module through the following code in config/initializers/textilizepatch.rb

module ActionView
  module Helpers
    module TextHelper
      def textilize(text, *options)
        options = [:hard_breaks] if options == nil || options.size == 0
 
        if text.blank?
          ""
        else
          textilized = RedCloth.new(text, options)
          textilized.to_html
        end
      end
    end
  end
end

Such changes should be tested… *grml*

| Comments (0) »

24-Sep-09