/*********************************************	Copyright © 2001 Jason Lamport.	All rights reserved.----------------------*******************************************/package com.strangelight.mactoolbox;import com.apple.jdirect.*;import com.apple.mrj.jdirect.*;/** *	Extends J_HandleStruct by providing an additional constructor *	for allocating a new Handle, and automatically disposing of its *	handle on finalize() (regardless of whether the handle was originally *	created by the J_DisposableHandle object or not) *	 *	Be careful *not* to let this object get garbage-collected as long as *	someone, somewhere may need to access the handle that it encapsulates. */public class J_DisposableHandle extends J_HandleStruct {	public static class NewHandle {		private int size;		public NewHandle(int s) {			size = s;		}		public int get_size() {			return size;		}	}	public J_DisposableHandle(int new_handle) {		super( new_handle );	}	public J_DisposableHandle( NewHandle newH ) throws E_OSErr {		super( allocate_handle( newH.get_size() ) );		handle_size = newH.get_size();	}	public void finalize() {		if ( handle != 0 ) {			MemoryFunctions.DisposeHandle( handle );		}	}		protected static int allocate_handle( int size ) throws E_OSErr {		int osErr = 0;		int new_hand = 0;						new_hand = MemoryFunctions.NewHandle( size );		if ( new_hand == 0 ) { 			osErr = MemoryFunctions.MemError();			if ( osErr == ErrorConstants.memFullErr ) {				/*					Run the garbage collector, then try again:				 */				System.gc();				new_hand = MemoryFunctions.NewHandle( size );				if ( new_hand == 0 ) { 					throw new E_OSErr( 						"Can't allocate new Handle for J_HandleStruct (gc() failed?)",						MemoryFunctions.MemError()					);				}			} else {				//	unknown error:				throw new E_OSErr( 					"Can't allocate new Handle for J_HandleStruct because of an unknown error.",					osErr				);			}		}		return new_hand;	}}