Mercurial > hg4j
diff 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 |
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/internal/EncodingHelper.java Fri Jan 11 18:10:29 2013 +0100 +++ b/src/org/tmatesoft/hg/internal/EncodingHelper.java Fri Jan 11 18:12:39 2013 +0100 @@ -65,8 +65,36 @@ // perhaps, can return byte[0] in this case? throw new IllegalArgumentException(); } + return encodeWithSystemDefaultFallback(s); + } + + /** + * Translate file names from dirstate to amazing Unicode string + */ + public String fromDirstate(byte[] data, int start, int length) { + return decodeWithSystemDefaultFallback(data, start, length); + } + + public byte[] toDirstate(String fname) { + if (fname == null) { + throw new IllegalArgumentException(); + } + return encodeWithSystemDefaultFallback(fname); + } + + private String decodeWithSystemDefaultFallback(byte[] data, int start, int length) { try { - // synchonized(encoder) { + return decoder.decode(ByteBuffer.wrap(data, start, length)).toString(); + } catch (CharacterCodingException ex) { + sessionContext.getLog().dump(getClass(), Error, ex, String.format("Use of charset %s failed, resort to system default", charset().name())); + // resort to system-default + return new String(data, start, length); + } + } + + private byte[] encodeWithSystemDefaultFallback(String s) { + try { + // synchronized(encoder) { ByteBuffer bb = encoder.encode(CharBuffer.wrap(s)); // } byte[] rv = new byte[bb.remaining()]; @@ -79,23 +107,6 @@ } } - /** - * Translate file names from dirstate to amazing Unicode string - */ - public String fromDirstate(byte[] data, int start, int length) { - return decodeWithSystemDefaultFallback(data, start, length); - } - - private String decodeWithSystemDefaultFallback(byte[] data, int start, int length) { - try { - return decoder.decode(ByteBuffer.wrap(data, start, length)).toString(); - } catch (CharacterCodingException ex) { - sessionContext.getLog().dump(getClass(), Error, ex, String.format("Use of charset %s failed, resort to system default", charset().name())); - // resort to system-default - return new String(data, start, length); - } - } - private Charset charset() { return encoder.charset(); }