% length - length of finite signal
function n=length(sig,varargin)
	opts=prefs('chunk',1024,'init',2,'grow',2,'max',1e12,varargin{:});

	s=construct(sig);
	try % to make sure we dispose of s once opened
		chunk=uint64(opts.chunk);
		n=uint64(0); CHUNK=1:chunk; 
		r=s.reader(opts.chunk);
		dummy=[];
		rem=0; 
		while rem==0
			[dummy,rem]=r();
			n=n+chunk;
			if n>opts.max, error('maximum length exceeded'); end
		end
		n=n-uint64(rem); % remove rem samples from end
	catch ex
		s.dispose();
		rethrow(ex);
	end
	s.dispose();
	n=double(n);
end
