Skip to content

Commit

Permalink
#132: InputAsLSInput
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jun 14, 2017
1 parent 178d4fa commit 0359abd
Showing 1 changed file with 148 additions and 0 deletions.
148 changes: 148 additions & 0 deletions src/main/java/org/cactoos/io/InputAsLSInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import org.cactoos.Input;
import org.cactoos.text.BytesAsText;
import org.w3c.dom.ls.LSInput;

/**
* Input as LSInput.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 0.6
* @checkstyle AbbreviationAsWordInNameCheck (10 lines)
*/
public final class InputAsLSInput implements LSInput {

/**
* The input.
*/
private final Input input;

/**
* Ctor.
* @param inpt Input
*/
public InputAsLSInput(final Input inpt) {
this.input = inpt;
}

@Override
public Reader getCharacterStream() {
return new InputStreamReader(this.getByteStream());
}

@Override
public void setCharacterStream(final Reader stream) {
// nothing to do
}

@Override
public InputStream getByteStream() {
try {
return this.input.stream();
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
}

@Override
public void setByteStream(final InputStream stream) {
// nothing to do
}

@Override
public String getStringData() {
try {
return new BytesAsText(new InputAsBytes(this.input)).asString();
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
}

@Override
public void setStringData(final String data) {
// nothing to do
}

@Override
public String getSystemId() {
return "#system";
}

@Override
public void setSystemId(final String sid) {
// nothing to do
}

@Override
public String getPublicId() {
return "#public";
}

@Override
public void setPublicId(final String pid) {
// nothing to do
}

@Override
public String getBaseURI() {
return "#base";
}

@Override
public void setBaseURI(final String uri) {
// nothing to do
}

@Override
public String getEncoding() {
return StandardCharsets.UTF_8.displayName();
}

@Override
public void setEncoding(final String encoding) {
// nothing to do
}

@Override
@SuppressWarnings("PMD.BooleanGetMethodName")
public boolean getCertifiedText() {
return true;
}

@Override
public void setCertifiedText(final boolean text) {
// nothing to do
}
}

0 comments on commit 0359abd

Please sign in to comment.