comparison src/org/tmatesoft/hg/internal/EncodingHelper.java @ 525:0be5be8d57e9

Repository checkout support, first iteration
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 11 Jan 2013 18:12:39 +0100
parents 909306e412e2
children 2f9ed6bcefa2
comparison
equal deleted inserted replaced
524:57b2c9eb3c69 525:0be5be8d57e9
63 public byte[] toManifest(String s) { 63 public byte[] toManifest(String s) {
64 if (s == null) { 64 if (s == null) {
65 // perhaps, can return byte[0] in this case? 65 // perhaps, can return byte[0] in this case?
66 throw new IllegalArgumentException(); 66 throw new IllegalArgumentException();
67 } 67 }
68 return encodeWithSystemDefaultFallback(s);
69 }
70
71 /**
72 * Translate file names from dirstate to amazing Unicode string
73 */
74 public String fromDirstate(byte[] data, int start, int length) {
75 return decodeWithSystemDefaultFallback(data, start, length);
76 }
77
78 public byte[] toDirstate(String fname) {
79 if (fname == null) {
80 throw new IllegalArgumentException();
81 }
82 return encodeWithSystemDefaultFallback(fname);
83 }
84
85 private String decodeWithSystemDefaultFallback(byte[] data, int start, int length) {
68 try { 86 try {
69 // synchonized(encoder) { 87 return decoder.decode(ByteBuffer.wrap(data, start, length)).toString();
88 } catch (CharacterCodingException ex) {
89 sessionContext.getLog().dump(getClass(), Error, ex, String.format("Use of charset %s failed, resort to system default", charset().name()));
90 // resort to system-default
91 return new String(data, start, length);
92 }
93 }
94
95 private byte[] encodeWithSystemDefaultFallback(String s) {
96 try {
97 // synchronized(encoder) {
70 ByteBuffer bb = encoder.encode(CharBuffer.wrap(s)); 98 ByteBuffer bb = encoder.encode(CharBuffer.wrap(s));
71 // } 99 // }
72 byte[] rv = new byte[bb.remaining()]; 100 byte[] rv = new byte[bb.remaining()];
73 bb.get(rv, 0, rv.length); 101 bb.get(rv, 0, rv.length);
74 return rv; 102 return rv;
77 // resort to system-default 105 // resort to system-default
78 return s.getBytes(); 106 return s.getBytes();
79 } 107 }
80 } 108 }
81 109
82 /**
83 * Translate file names from dirstate to amazing Unicode string
84 */
85 public String fromDirstate(byte[] data, int start, int length) {
86 return decodeWithSystemDefaultFallback(data, start, length);
87 }
88
89 private String decodeWithSystemDefaultFallback(byte[] data, int start, int length) {
90 try {
91 return decoder.decode(ByteBuffer.wrap(data, start, length)).toString();
92 } catch (CharacterCodingException ex) {
93 sessionContext.getLog().dump(getClass(), Error, ex, String.format("Use of charset %s failed, resort to system default", charset().name()));
94 // resort to system-default
95 return new String(data, start, length);
96 }
97 }
98
99 private Charset charset() { 110 private Charset charset() {
100 return encoder.charset(); 111 return encoder.charset();
101 } 112 }
102 113
103 } 114 }