/* DefaultPointerIndexType.java * * Copyright (c) 2003, Benja Fallenstein * * This file is part of Storm. * * Storm is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Storm is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * You should have received a copy of the GNU Lesser General * Public License along with Storm; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * */ /* * Written by Benja Fallenstein */ package org.nongnu.storm.impl; import org.nongnu.storm.*; import org.nongnu.storm.util.HexUtil; import java.io.*; import java.util.*; public class DefaultPointerIndexType extends PointerIndexType { /** The static instance of this singleton. */ public static final DefaultPointerIndexType type = new DefaultPointerIndexType(); protected DefaultPointerIndexType() { } public Object createIndex(IndexedPool pool, IndexedPool.DB db) { return new DefaultPointerIndex(pool, db); } public Set getMappings(Block b) throws IOException { String blockContentType = b.getHeader().get("Content-Type").toLowerCase(); if(blockContentType.equals(contentType)) { BufferedReader br = new BufferedReader( new InputStreamReader(b.getInputStream(), "ISO8859_1")); String s = br.readLine(); if(!s.equals("GZZPTR0")) { // Unknown pointer block format-- ignore return Collections.EMPTY_SET; } String pointerURI = br.readLine(); byte[] key = pointerURI.getBytes("US-ASCII"); List items = new ArrayList(3); int len = 0; String hex; while((hex = br.readLine()) != null) { byte[] arr = HexUtil.hexToByteArr(hex); if(arr.length > 127) throw new IOException("Storm id is too long: "+hex); items.add(arr); len += arr.length + 1; } if(len == 0) throw new IOException("Pointer block without target line"); br.close(); byte[] value = new byte[len]; int pos = 0; for(Iterator i=items.iterator(); i.hasNext();) { byte[] arr = (byte[])i.next(); value[pos] = (byte)arr.length; pos++; System.arraycopy(arr, 0, value, pos, arr.length); pos += arr.length; } IndexedPool.Mapping m = new IndexedPool.Mapping(b.getId(), key, value); return Collections.singleton(m); } else { return Collections.EMPTY_SET; } } }