Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Jan 4, 2025
2 parents e100d62 + 15a38a4 commit b63a788
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
37 changes: 21 additions & 16 deletions src/main/java/org/apache/commons/csv/CSVFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
* </p>
*
* <pre>{@code
* CSVFormat.EXCEL.withNullString("N/A").withIgnoreSurroundingSpaces(true);
* CSVFormat.EXCEL.builder().setNullString("N/A").setIgnoreSurroundingSpaces(true).get();
* }</pre>
*
* <h2>Defining column names</h2>
Expand All @@ -103,7 +103,7 @@
* </p>
*
* <pre>{@code
* CSVFormat.EXCEL.withHeader("Col1", "Col2", "Col3");
* CSVFormat.EXCEL.builder().setHeader("Col1", "Col2", "Col3").get();
* }</pre>
*
* <p>
Expand All @@ -122,7 +122,7 @@
*
* <pre>{@code
* Reader in = ...;
* CSVFormat.EXCEL.withHeader("Col1", "Col2", "Col3").parse(in);
* CSVFormat.EXCEL.builder().setHeader("Col1", "Col2", "Col3").get().parse(in);
* }</pre>
*
* <p>
Expand All @@ -137,7 +137,7 @@
* </p>
*
* <pre>
* CSVFormat.EXCEL.withHeader();
* CSVFormat.EXCEL.builder().setHeader().get();
* </pre>
*
* <p>
Expand Down Expand Up @@ -993,7 +993,7 @@ public CSVFormat getFormat() {
* </p>
*
* <pre>
* CSVFormat fmt = CSVFormat.EXCEL.withDelimiter(';');
* CSVFormat fmt = CSVFormat.EXCEL.builder().setDelimiter(';').get();
* </pre>
*
* <p>
Expand Down Expand Up @@ -2693,6 +2693,7 @@ public CSVFormat withEscape(final Character escape) {
return builder().setEscape(escape).get();
}

// @formatter:off
/**
* Builds a new {@code CSVFormat} using the first record as header.
*
Expand All @@ -2701,7 +2702,10 @@ public CSVFormat withEscape(final Character escape) {
* </p>
*
* <pre>
* CSVFormat format = aFormat.withHeader().withSkipHeaderRecord();
* CSVFormat format = aFormat.builder()
* .setHeader()
* .setSkipHeaderRecord(true)
* .get();
* </pre>
*
* @return A new CSVFormat that is equal to this but using the first record as header.
Expand All @@ -2710,6 +2714,7 @@ public CSVFormat withEscape(final Character escape) {
* @since 1.3
* @deprecated Use {@link Builder#setHeader(String...) Builder#setHeader()}.{@link Builder#setSkipHeaderRecord(boolean) setSkipHeaderRecord(true)}.
*/
// @formatter:on
@Deprecated
public CSVFormat withFirstRecordAsHeader() {
// @formatter:off
Expand All @@ -2728,11 +2733,11 @@ public CSVFormat withFirstRecordAsHeader() {
* </p>
*
* <pre>
* public enum Header {
* public enum MyHeader {
* Name, Email, Phone
* }
*
* CSVFormat format = aformat.withHeader(Header.class);
* ...
* CSVFormat format = aFormat.builder().setHeader(MyHeader.class).get();
* </pre>
* <p>
* The header is also used by the {@link CSVPrinter}.
Expand All @@ -2755,13 +2760,13 @@ public CSVFormat withHeader(final Class<? extends Enum<?>> headerEnum) {
* input file with:
*
* <pre>
* CSVFormat format = aformat.withHeader();
* CSVFormat format = aFormat.builder().setHeader().get();
* </pre>
*
* or specified manually with:
*
* <pre>
* CSVFormat format = aformat.withHeader(resultSet);
* CSVFormat format = aFormat.builder().setHeader(resultSet).get();
* </pre>
* <p>
* The header is also used by the {@link CSVPrinter}.
Expand All @@ -2783,13 +2788,13 @@ public CSVFormat withHeader(final ResultSet resultSet) throws SQLException {
* input file with:
*
* <pre>
* CSVFormat format = aformat.withHeader();
* CSVFormat format = aFormat.builder().setHeader().get()
* </pre>
*
* or specified manually with:
*
* <pre>
* CSVFormat format = aformat.withHeader(metaData);
* CSVFormat format = aFormat.builder().setHeader(resultSetMetaData).get()
* </pre>
* <p>
* The header is also used by the {@link CSVPrinter}.
Expand All @@ -2811,13 +2816,13 @@ public CSVFormat withHeader(final ResultSetMetaData resultSetMetaData) throws SQ
* with:
*
* <pre>
* CSVFormat format = aformat.withHeader();
* CSVFormat format = aFormat.builder().setHeader().get();
* </pre>
*
* or specified manually with:
*
* <pre>{@code
* CSVFormat format = aformat.withHeader("name", "email", "phone");
* CSVFormat format = aFormat.builder().setHeader("name", "email", "phone").get();
* }</pre>
* <p>
* The header is also used by the {@link CSVPrinter}.
Expand All @@ -2838,7 +2843,7 @@ public CSVFormat withHeader(final String... header) {
* This setting is ignored by the parser.
*
* <pre>{@code
* CSVFormat format = aformat.withHeaderComments("Generated by Apache Commons CSV.", Instant.now());
* CSVFormat format = aFormat.builder().setHeaderComments("Generated by Apache Commons CSV.", Instant.now()).get();
* }</pre>
*
* @param headerComments the headerComments which will be printed by the Printer before the actual CSV data.
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ final class ExtendedBufferedReader extends UnsynchronizedBufferedReader {
private long bytesReadMark;

/** Encoder for calculating the number of bytes for each character read. */
private CharsetEncoder encoder;
private final CharsetEncoder encoder;

/**
* Constructs a new instance using the default buffer size.
*/
ExtendedBufferedReader(final Reader reader) {
super(reader);
this(reader, null, false);
}

/**
Expand All @@ -80,9 +80,7 @@ final class ExtendedBufferedReader extends UnsynchronizedBufferedReader {
*/
ExtendedBufferedReader(final Reader reader, final Charset charset, final boolean trackBytes) {
super(reader);
if (charset != null && trackBytes) {
encoder = charset.newEncoder();
}
encoder = charset != null && trackBytes ? charset.newEncoder() : null;
}

/**
Expand Down Expand Up @@ -137,15 +135,13 @@ private int getEncodedCharLength(final int current) throws CharacterCodingExcept
final char cChar = (char) current;
final char lChar = (char) lastChar;
if (!Character.isSurrogate(cChar)) {
return encoder.encode(
CharBuffer.wrap(new char[] {cChar})).limit();
return encoder.encode(CharBuffer.wrap(new char[] { cChar })).limit();
}
if (Character.isHighSurrogate(cChar)) {
// Move on to the next char (low surrogate)
return 0;
} else if (Character.isSurrogatePair(lChar, cChar)) {
return encoder.encode(
CharBuffer.wrap(new char[] {lChar, cChar})).limit();
return encoder.encode(CharBuffer.wrap(new char[] { lChar, cChar })).limit();
} else {
throw new CharacterCodingException();
}
Expand Down

0 comments on commit b63a788

Please sign in to comment.