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

IO-568: AutoCloseInputStream should disable mark/reset #55

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,36 @@
* stream as soon as possible even if the client application (by not explicitly
* closing the stream when no longer needed) or the underlying stream (by not
* releasing resources once the last byte has been read) do not do that.
* <p>
* Since there is no safe way to always close the stream when reaching the end and
* respecting mark/reset contract, it's possible to force disabled mark support.
* This is highly recommended when you don't know how the stream is going to be used
* (mark is not disabled by default for retro compatibility reasons).
*
* @since 1.4
*/
public class AutoCloseInputStream extends ProxyInputStream {

private boolean markEnabled = true;

/**
* Creates an automatically closing proxy for the given input stream.
*
* @param in underlying input stream
*/
public AutoCloseInputStream(final InputStream in) {
this(in, true);
}

/**
* Creates an automatically closing proxy for the given input stream.
*
* @param in underlying input stream
*/
public AutoCloseInputStream(final InputStream in, boolean markEnabled) {
super(in);

this.markEnabled = markEnabled;
}

/**
Expand Down Expand Up @@ -79,6 +97,33 @@ protected void afterRead(final int n) throws IOException {
}
}

@Override
public synchronized void mark(int readlimit) {
if (this.markEnabled) {
super.mark(readlimit);
}
}

@Override
public synchronized void reset() throws IOException {
if (this.markEnabled) {
super.reset();
} else {
// Behave as standard InputStream not supporting mark/reset
throw new IOException("mark/reset not supported");
}
}

@Override
public boolean markSupported() {
if (this.markEnabled) {
return super.markSupported();
} else {
// Behave as standard InputStream not supporting mark/reset
return false;
}
}

/**
* Ensures that the stream is closed before it gets garbage-collected.
* As mentioned in {@link #close()}, this is a no-op if the stream has
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.commons.io.input;

import java.io.IOException;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* JUnit Test Case for {@link AutoCloseInputStream} with enabled mark support (the default).
*/
public class MarkEnabledAutoCloseInputStreamTest extends AbstractAutoCloseInputStreamTest {

public MarkEnabledAutoCloseInputStreamTest() {
super(true);
}

@Test
public void testMark() throws IOException {
assertTrue(targetStream.markSupported());

// Make sure mark is disabled
assertTrue(stream.markSupported());

// Check that mark() does not fail
stream.mark(1);
assertTrue("not marked", marked);

assertEquals('x', this.stream.read());

// Check that reset() works
stream.reset();
assertTrue("not reseted", reseted);

assertEquals('x', this.stream.read());
}
}