}
}
protected void saveOnlyIfChangedWithMemoryBuffer(Map<?, ?> options) throws IOException
{
URIConverter uriConverter = getURIConverter();
class MyByteArrayOutputStream extends ByteArrayOutputStream
{
public byte[] buffer()
{
return buf;
}
public int length()
{
return count;
}
}
MyByteArrayOutputStream memoryBuffer = new MyByteArrayOutputStream();
try
{
save(memoryBuffer, options);
}
finally
{
memoryBuffer.close();
}
byte [] newContentBuffer = memoryBuffer.buffer();
int length = memoryBuffer.length();
ByteArrayInputStream inputStream = new ByteArrayInputStream(newContentBuffer);
InputStream underlyingInputStream = getUnderlyingInputStream(inputStream, options);
byte [] underlyingNewContentBuffer;
int underlyingLength;
if (inputStream == underlyingInputStream)
{
underlyingNewContentBuffer = newContentBuffer;
underlyingLength = length;
}
else
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
byte [] buffer = new byte[4000];
for (int count = underlyingInputStream.read(buffer); count > 0; count = underlyingInputStream.read(buffer))
{
bytes.write(buffer, 0, count);
}
bytes.close();
underlyingInputStream.close();
underlyingNewContentBuffer = bytes.toByteArray();
underlyingLength = underlyingNewContentBuffer.length;
}
boolean equal = true;
InputStream oldContents = null;
try
{
oldContents = getUnderlyingInputStream(uriConverter.createInputStream(getURI(), defaultLoadOptions), options);
}
catch (IOException exception)
{
equal = false;
}
if (oldContents != null)
{
try
{
byte [] oldContentBuffer = new byte [underlyingLength];
int count = oldContents.read(oldContentBuffer);
while (count > 0 && count < underlyingLength)
{
int more = oldContents.read(oldContentBuffer, count, oldContentBuffer.length - count);
if (more <= 0)
{
break;
}
else
{
count += more;
}
}
if (count == underlyingLength && oldContents.read() == -1)
{
for (int i = 0; i < underlyingLength; ++i)
{
if (oldContentBuffer[i] != underlyingNewContentBuffer[i])
{
equal = false;
break;
}
}
}
else
{
equal = false;
}
}
finally
{
oldContents.close();
}
}
if (!equal)
{
Map<?, ?> response = options == null ? null : (Map<?, ?>)options.get(URIConverter.OPTION_RESPONSE);
if (response == null)
{
response = new HashMap<Object, Object>();
}
OutputStream newContents = uriConverter.createOutputStream(getURI(), new ExtensibleURIConverterImpl.OptionsMap(URIConverter.OPTION_RESPONSE, response, options));
try
{
newContents.write(newContentBuffer, 0, length);
}
finally