Index: src/uk/me/parabola/imgfmt/app/BufferedImgFileWriter.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/BufferedImgFileWriter.java	(revision 3595)
+++ src/uk/me/parabola/imgfmt/app/BufferedImgFileWriter.java	(working copy)
@@ -191,7 +191,9 @@
 		if (needed > (bufferSize - GUARD_SIZE)) {
 			while(needed > (bufferSize - GUARD_SIZE))
 				bufferSize += GROW_SIZE;
-			if (bufferSize > maxAllowedSize) {
+			if (bufferSize > maxAllowedSize && maxAllowedSize <= Integer.MAX_VALUE) 
+				bufferSize = (int) maxAllowedSize;
+			if (bufferSize > maxAllowedSize || needed > (bufferSize - GUARD_SIZE)) {
 				// Previous message was confusing people, although it is difficult to come
 				// up with something that is strictly true in all situations.
 				throw new MapFailedException(
