545 public static Reader newReader(ReadableByteChannel ch, |
545 public static Reader newReader(ReadableByteChannel ch, |
546 String csName) |
546 String csName) |
547 { |
547 { |
548 Objects.requireNonNull(csName, "csName"); |
548 Objects.requireNonNull(csName, "csName"); |
549 return newReader(ch, Charset.forName(csName).newDecoder(), -1); |
549 return newReader(ch, Charset.forName(csName).newDecoder(), -1); |
|
550 } |
|
551 |
|
552 /** |
|
553 * Constructs a reader that decodes bytes from the given channel according |
|
554 * to the given charset. |
|
555 * |
|
556 * <p> An invocation of this method of the form |
|
557 * |
|
558 * <pre> {@code |
|
559 * Channels.newReader(ch, charset) |
|
560 * } </pre> |
|
561 * |
|
562 * behaves in exactly the same way as the expression |
|
563 * |
|
564 * <pre> {@code |
|
565 * Channels.newReader(ch, Charset.forName(csName).newDecoder(), -1) |
|
566 * } </pre> |
|
567 * |
|
568 * <p> The reader's default action for malformed-input and unmappable-character |
|
569 * errors is to {@linkplain java.nio.charset.CodingErrorAction#REPORT report} |
|
570 * them. When more control over the error handling is required, the constructor |
|
571 * that takes a {@linkplain java.nio.charset.CharsetDecoder} should be used. |
|
572 * |
|
573 * @param ch The channel from which bytes will be read |
|
574 * |
|
575 * @param charset The charset to be used |
|
576 * |
|
577 * @return A new reader |
|
578 */ |
|
579 public static Reader newReader(ReadableByteChannel ch, Charset charset) { |
|
580 Objects.requireNonNull(charset, "charset"); |
|
581 return newReader(ch, charset.newDecoder(), -1); |
550 } |
582 } |
551 |
583 |
552 /** |
584 /** |
553 * Constructs a writer that encodes characters using the given encoder and |
585 * Constructs a writer that encodes characters using the given encoder and |
554 * writes the resulting bytes to the given channel. |
586 * writes the resulting bytes to the given channel. |
614 String csName) |
646 String csName) |
615 { |
647 { |
616 Objects.requireNonNull(csName, "csName"); |
648 Objects.requireNonNull(csName, "csName"); |
617 return newWriter(ch, Charset.forName(csName).newEncoder(), -1); |
649 return newWriter(ch, Charset.forName(csName).newEncoder(), -1); |
618 } |
650 } |
|
651 |
|
652 /** |
|
653 * Constructs a writer that encodes characters according to the given |
|
654 * charset and writes the resulting bytes to the given channel. |
|
655 * |
|
656 * <p> An invocation of this method of the form |
|
657 * |
|
658 * <pre> {@code |
|
659 * Channels.newWriter(ch, charset) |
|
660 * } </pre> |
|
661 * |
|
662 * behaves in exactly the same way as the expression |
|
663 * |
|
664 * <pre> {@code |
|
665 * Channels.newWriter(ch, Charset.forName(csName).newEncoder(), -1) |
|
666 * } </pre> |
|
667 * |
|
668 * <p> The writer's default action for malformed-input and unmappable-character |
|
669 * errors is to {@linkplain java.nio.charset.CodingErrorAction#REPORT report} |
|
670 * them. When more control over the error handling is required, the constructor |
|
671 * that takes a {@linkplain java.nio.charset.CharsetEncoder} should be used. |
|
672 * |
|
673 * @param ch |
|
674 * The channel to which bytes will be written |
|
675 * |
|
676 * @param charset |
|
677 * The charset to be used |
|
678 * |
|
679 * @return A new writer |
|
680 */ |
|
681 public static Writer newWriter(WritableByteChannel ch, Charset charset) { |
|
682 Objects.requireNonNull(charset, "charset"); |
|
683 return newWriter(ch, charset.newEncoder(), -1); |
619 } |
684 } |
|
685 } |