001    /*
002     * This file is part of the Tuning Fork Visualization Platform
003     *  (http://sourceforge.net/projects/tuningforkvp)
004     *
005     * Copyright (c) 2005 - 2008 IBM Corporation.
006     * All rights reserved. This program and the accompanying materials
007     * are made available under the terms of the Eclipse Public License v1.0
008     * which accompanies this distribution, and is available at
009     * http://www.eclipse.org/legal/epl-v10.html
010     *
011     * Contributors:
012     *     IBM Corporation - initial API and implementation
013     */
014    
015    package com.ibm.tuningfork.tracegen.chunk;
016    
017    import org.vmmagic.pragma.Uninterruptible;
018    
019    @Uninterruptible
020    public class StringTableChunk extends Chunk {
021    
022        public static final int STRING_TABLE_ID = 6;
023        public static final int STRING_COUNT_OFFSET = Chunk.DATA_OFFSET;
024        public static final int STRING_DATA_OFFSET = STRING_COUNT_OFFSET + 4;
025        private int numberOfStrings = 0;
026    
027        public StringTableChunk() {
028            super(STRING_TABLE_ID);
029            seek(STRING_DATA_OFFSET);
030        }
031    
032        public boolean add(int index, String val) {
033            int guess = ENCODING_SPACE_INT + JikesRVMSupport.getStringLength(val);
034            if (!hasRoom(guess)) {
035                return false;
036            }
037            int savedPosition = getPosition();
038            addInt(index);
039            if (!addString(val)) {
040                seek(savedPosition);
041                return false;
042            }
043            numberOfStrings++;
044            return true;
045        }
046    
047        @Override
048        public void close() {
049            putIntAt(STRING_COUNT_OFFSET, numberOfStrings);
050            numberOfStrings = 0;
051            super.close();
052        }
053    
054        public boolean hasData() {
055            return numberOfStrings > 0;
056        }
057    
058        public void reset() {
059            resetImpl();
060            numberOfStrings = 0;
061            seek(STRING_DATA_OFFSET);
062        }
063    }