private byte[] decompress(Sprite s) throws IOException {		
		
		byte[] buffer = new byte[0];
				
		// Size of data in file
		int compsize = 0;
		
		// uncompressed size
		int datasize = 0;
		
		int testsize;
				
		// while we still need data
		while (true) {
			
			if (s.hasFlag(SpriteInfo.Compressed)) {
				testsize = compsize;
			} else {
				testsize = datasize;
			}
			
			if (testsize >= s.getImageSize()) {
				break;
			}		
			
			int code = file.readByte();				
			compsize++;
			
			if ((code & 128) == 128) {
				int ofs = file.readUnsignedByte();
				compsize++;
				long length = -(code >> 3);				
				long offset = ((code & 7) << 8) | ofs;					
				
				if (datasize < offset) {						
					throw new IOException();
				}
								
				byte[] bytes = new byte[(int)length];
								
				try {
					for (int i = 0; i < length; i++) {
						int x = (int) ((buffer.length - offset) + i);
						bytes[i] = buffer[x];
					}					
				} catch (ArrayIndexOutOfBoundsException e) {
					System.err.println("out of bounds");
				}
				
				buffer = ArrayUtils.addAll(buffer, bytes);				
				datasize += length;
				
			}
			else {				
				int readsize = code; 				
				if (code == 0) { readsize = 128; }
				
				byte[] bytes = new byte[readsize];				
				file.read(bytes);
				buffer = ArrayUtils.addAll(buffer, bytes);
				
				datasize += readsize;
				compsize += readsize;
			}						
			
		} 
		
		return buffer;
	}