-
Notifications
You must be signed in to change notification settings - Fork 12
#372: Read properties files as UTF-8 #373
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
Draft
jowerner
wants to merge
3
commits into
develop
Choose a base branch
from
372-Read-properties-files-as-UTF-8
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * Copyright (c) 2005-2023 Xceptance Software Technologies GmbH | ||
| * | ||
| * 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. | ||
| */ | ||
| package com.xceptance.common.io; | ||
|
|
||
| import java.io.CharArrayReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.Reader; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.CharBuffer; | ||
| import java.nio.charset.CharacterCodingException; | ||
| import java.nio.charset.Charset; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| /** | ||
| * A {@link Reader} implementation that reads UTF-8-encoded text from an {@link InputStream} throwing an | ||
| * {@link IOException} if the text is not valid UTF-8. | ||
| * <p> | ||
| * Note: This class buffers the content of the stream in memory so it should be used for small amounts of data only. | ||
| */ | ||
| public class Utf8Reader extends Reader | ||
| { | ||
| /** | ||
| * The reader we delegate to when actually dealing out the characters. | ||
| */ | ||
| private final CharArrayReader charArrayReader; | ||
|
|
||
| /** | ||
| * Creates a new {@link Utf8Reader} instance. | ||
| * | ||
| * @param inputStream | ||
| * the stream to read the text from | ||
| * @throws IOException | ||
| * if the input stream could not be read or does not represent UTF-8-encoded text | ||
| */ | ||
| public Utf8Reader(final InputStream inputStream) throws IOException | ||
| { | ||
| final byte[] bytes = inputStream.readAllBytes(); | ||
| char[] chars; | ||
|
|
||
| try | ||
| { | ||
| chars = getAsChars(bytes, StandardCharsets.UTF_8); | ||
| } | ||
| catch (final CharacterCodingException cce) | ||
| { | ||
| throw new IOException("Data does not represent UTF-8-encoded text", cce); | ||
| } | ||
|
|
||
| charArrayReader = new CharArrayReader(chars); | ||
h-arlt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Converts the given bytes to chars according to the specified character set. | ||
| * | ||
| * @param bytes | ||
| * the input bytes | ||
| * @param charset | ||
| * the character set | ||
| * @return the corresponding chars | ||
| * @throws CharacterCodingException | ||
| * if the bytes do not represent text encoded with the given character set | ||
| */ | ||
| private static char[] getAsChars(final byte[] bytes, final Charset charset) throws CharacterCodingException | ||
| { | ||
| final CharBuffer charBuffer = charset.newDecoder().decode(ByteBuffer.wrap(bytes)); | ||
|
|
||
| final char[] chars = new char[charBuffer.length()]; | ||
| charBuffer.get(chars); | ||
|
|
||
| return chars; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public int read(final char[] cbuf, final int off, final int len) throws IOException | ||
| { | ||
| return charArrayReader.read(cbuf, off, len); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public void close() throws IOException | ||
| { | ||
| charArrayReader.close(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * Copyright (c) 2005-2023 Xceptance Software Technologies GmbH | ||
| * | ||
| * 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. | ||
| */ | ||
| package com.xceptance.common.io; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.io.Reader; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
|
|
||
| import junitparams.JUnitParamsRunner; | ||
| import junitparams.Parameters; | ||
|
|
||
| /** | ||
| * Tests the implementation of {@link Utf8Reader}. | ||
| */ | ||
| @RunWith(JUnitParamsRunner.class) | ||
| public class Utf8ReaderTest | ||
| { | ||
| /** | ||
| * Checks that the reader can read test texts encoded with the given encodings. | ||
| */ | ||
| @Test | ||
| @Parameters(value = | ||
| { | ||
| "UTF-8 | 日本の東京", // | ||
| "UTF-8 | ッ ツ ヅ ミ べ ボ プ", // | ||
| "UTF-8 | äöüÄÖÜßáàÁÀ", // | ||
| "UTF-8 | foobar", // | ||
| "ISO-8859-1 | foobar", // same bytes as with UTF-8 | ||
| "US-ASCII | foobar", // same bytes as with UTF-8 | ||
| }) | ||
| public void read(final String charsetName, final String text) throws IOException | ||
| { | ||
| doRead(charsetName, text); | ||
| } | ||
|
|
||
| /** | ||
| * Checks that the reader throws an IOException for test texts encoded with the given encodings. | ||
| */ | ||
| @Test(expected = IOException.class) | ||
| @Parameters(value = | ||
| { | ||
| "ISO-8859-1 | äöüÄÖÜßáàÁÀ", // | ||
| "UTF-16 | äöüÄÖÜßáàÁÀ", // | ||
| "UTF-16BE | äöüÄÖÜßáàÁÀ", // | ||
| "UTF-16LE | äöüÄÖÜßáàÁÀ", // | ||
| }) | ||
| public void read_illegalEncoding(final String charsetName, final String text) throws IOException | ||
| { | ||
| doRead(charsetName, text); | ||
| } | ||
|
|
||
| private void doRead(final String charsetName, final String text) throws IOException | ||
| { | ||
| // get the bytes of the text in the wanted encoding | ||
| final byte[] bytes = text.getBytes(charsetName); | ||
|
|
||
| // now read the bytes in again via the Utf8Reader and check the resulting text | ||
| try (final Reader reader = new Utf8Reader(new ByteArrayInputStream(bytes))) | ||
| { | ||
| final char[] chars = new char[1024]; | ||
| final int charsRead = reader.read(chars); | ||
|
|
||
| final String actualText = new String(chars, 0, charsRead); | ||
|
|
||
| Assert.assertEquals(text.length(), charsRead); | ||
| Assert.assertEquals(text, actualText); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.