comparison src/org/tmatesoft/hg/internal/EncodingHelper.java @ 418:528b6780a8bd

A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 22 Mar 2012 21:02:20 +0100
parents ee8264d80747
children 909306e412e2
comparison
equal deleted inserted replaced
417:ccd7d25e5aea 418:528b6780a8bd
46 sessionContext = ctx; 46 sessionContext = ctx;
47 decoder = fsEncoding.newDecoder(); 47 decoder = fsEncoding.newDecoder();
48 encoder = fsEncoding.newEncoder(); 48 encoder = fsEncoding.newEncoder();
49 } 49 }
50 50
51 /**
52 * Translate file names from manifest to amazing Unicode string
53 */
51 public String fromManifest(byte[] data, int start, int length) { 54 public String fromManifest(byte[] data, int start, int length) {
52 try { 55 return decodeWithSystemDefaultFallback(data, start, length);
53 return decoder.decode(ByteBuffer.wrap(data, start, length)).toString();
54 } catch (CharacterCodingException ex) {
55 sessionContext.getLog().error(getClass(), ex, String.format("Use of charset %s failed, resort to system default", charset().name()));
56 // resort to system-default
57 return new String(data, start, length);
58 }
59 } 56 }
60 57
61 /** 58 /**
62 * @return byte representation of the string directly comparable to bytes in manifest 59 * @return byte representation of the string directly comparable to bytes in manifest
63 */ 60 */
64 public byte[] toManifest(String s) { 61 public byte[] toManifest(String s) {
65 if (s == null) { 62 if (s == null) {
78 // resort to system-default 75 // resort to system-default
79 return s.getBytes(); 76 return s.getBytes();
80 } 77 }
81 } 78 }
82 79
83 public String fromDirstate(byte[] data, int start, int length) throws CharacterCodingException { // FIXME perhaps, log is enough, and charset() may be private? 80 /**
84 return decoder.decode(ByteBuffer.wrap(data, start, length)).toString(); 81 * Translate file names from dirstate to amazing Unicode string
82 */
83 public String fromDirstate(byte[] data, int start, int length) {
84 return decodeWithSystemDefaultFallback(data, start, length);
85 } 85 }
86 86
87 public Charset charset() { 87 private String decodeWithSystemDefaultFallback(byte[] data, int start, int length) {
88 try {
89 return decoder.decode(ByteBuffer.wrap(data, start, length)).toString();
90 } catch (CharacterCodingException ex) {
91 sessionContext.getLog().error(getClass(), ex, String.format("Use of charset %s failed, resort to system default", charset().name()));
92 // resort to system-default
93 return new String(data, start, length);
94 }
95 }
96
97 private Charset charset() {
88 return encoder.charset(); 98 return encoder.charset();
89 } 99 }
90 100
91 } 101 }