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 015package com.ibm.tuningfork.tracegen.chunk; 016 017import org.jikesrvm.VM; 018import org.vmmagic.pragma.Interruptible; 019import org.vmmagic.pragma.Uninterruptible; 020 021@Uninterruptible 022public abstract class Chunk extends RawChunk { 023 024 private static final int MAGIC_WORD_1 = 0xdeadbeef; 025 private static final int MAGIC_WORD_2 = 0xcafebabe; 026 private static final int LENGTH_OFFSET = 8; 027 private static final int CHUNK_TYPE_OFFSET = 12; 028 protected static final int DATA_OFFSET = 16; 029 030 protected final static int DEFAULT_CHUNK_SIZE = 16 * 1024; 031 032 protected Chunk(int chunkType, byte[] buffer) { 033 super(buffer); 034 addInt(MAGIC_WORD_1); 035 addInt(MAGIC_WORD_2); 036 seek(CHUNK_TYPE_OFFSET); 037 addInt(chunkType); 038 seek(DATA_OFFSET); 039 } 040 041 protected Chunk(int chunkType, int capacity) { 042 this(chunkType, new byte[capacity]); 043 } 044 045 protected Chunk(int chunkType) { 046 this(chunkType, new byte[DEFAULT_CHUNK_SIZE]); 047 } 048 049 @Override 050 public void close() { 051 int pos = getPosition(); 052 int bodyLength = pos - DATA_OFFSET; 053 putIntAt(LENGTH_OFFSET, bodyLength); 054 super.close(); 055 } 056 057 @Override 058 protected void resetImpl() { 059 super.resetImpl(); 060 seek(DATA_OFFSET); 061 } 062 063 @Interruptible 064 protected char[] getChars(String s) { 065 if (VM.runningVM) { 066 return JikesRVMSupport.getBackingCharArray(s); 067 } else { 068 return s.toCharArray(); 069 } 070 } 071}