Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provides a CachingOuptutStream and a CachingWriter #184

Merged
merged 8 commits into from Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/main/java/org/codehaus/plexus/util/io/CachingOutputStream.java
@@ -0,0 +1,66 @@
package org.codehaus.plexus.util.io;

/*
* Copyright The Codehaus Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Objects;

/**
* Caching OutputStream to avoid overwriting a file with
* the same content.
*/
public class CachingOutputStream extends ByteArrayOutputStream
{
private final Path path;
gnodet marked this conversation as resolved.
Show resolved Hide resolved
gnodet marked this conversation as resolved.
Show resolved Hide resolved
private boolean modified;

public CachingOutputStream( File path )
{
this( Objects.requireNonNull( path ).toPath() );
}

public CachingOutputStream( Path path )
{
this.path = Objects.requireNonNull( path );
}

@Override
public void close() throws IOException
{
byte[] data = toByteArray();
if ( Files.exists( path ) && Files.size( path ) == data.length )
{
byte[] old = Files.readAllBytes( path );
if ( Arrays.equals( old, data ) )
gnodet marked this conversation as resolved.
Show resolved Hide resolved
{
return;
}
}
Files.write( path, data );
modified = true;
}

public boolean isModified()
{
return modified;
}
}
69 changes: 69 additions & 0 deletions src/main/java/org/codehaus/plexus/util/io/CachingWriter.java
@@ -0,0 +1,69 @@
package org.codehaus.plexus.util.io;

/*
* Copyright The Codehaus Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Objects;

/**
* Caching Writer to avoid overwriting a file with
* the same content.
*/
public class CachingWriter extends StringWriter
{
private final Path path;
private final Charset charset;
private boolean modified;

public CachingWriter( File path, Charset charset )
{
this( Objects.requireNonNull( path ).toPath(), charset );
}

public CachingWriter( Path path, Charset charset )
{
this.path = Objects.requireNonNull( path );
this.charset = Objects.requireNonNull( charset );
}

@Override
public void close() throws IOException
{
byte[] data = getBuffer().toString().getBytes( charset );
if ( Files.exists( path ) && Files.size( path ) == data.length )
{
byte[] ba = Files.readAllBytes( path );
if ( Arrays.equals( data, ba ) )
gnodet marked this conversation as resolved.
Show resolved Hide resolved
{
return;
}
}
Files.write( path, data );
modified = true;
}

public boolean isModified()
{
return modified;
}
}