comparison src/org/tmatesoft/hg/internal/EncodingHelper.java @ 526:2f9ed6bcefa2

Initial support for Revert command with accompanying minor refactoring
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 15 Jan 2013 17:07:19 +0100
parents 0be5be8d57e9
children 47b7bedf0569
comparison
equal deleted inserted replaced
525:0be5be8d57e9 526:2f9ed6bcefa2
1 /* 1 /*
2 * Copyright (c) 2011-2012 TMate Software Ltd 2 * Copyright (c) 2011-2013 TMate Software Ltd
3 * 3 *
4 * This program is free software; you can redistribute it and/or modify 4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by 5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License. 6 * the Free Software Foundation; version 2 of the License.
7 * 7 *
58 } 58 }
59 59
60 /** 60 /**
61 * @return byte representation of the string directly comparable to bytes in manifest 61 * @return byte representation of the string directly comparable to bytes in manifest
62 */ 62 */
63 public byte[] toManifest(String s) { 63 public byte[] toManifest(CharSequence 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); 68 return encodeWithSystemDefaultFallback(s);
73 */ 73 */
74 public String fromDirstate(byte[] data, int start, int length) { 74 public String fromDirstate(byte[] data, int start, int length) {
75 return decodeWithSystemDefaultFallback(data, start, length); 75 return decodeWithSystemDefaultFallback(data, start, length);
76 } 76 }
77 77
78 public byte[] toDirstate(String fname) { 78 public byte[] toDirstate(CharSequence fname) {
79 if (fname == null) { 79 if (fname == null) {
80 throw new IllegalArgumentException(); 80 throw new IllegalArgumentException();
81 } 81 }
82 return encodeWithSystemDefaultFallback(fname); 82 return encodeWithSystemDefaultFallback(fname);
83 } 83 }
90 // resort to system-default 90 // resort to system-default
91 return new String(data, start, length); 91 return new String(data, start, length);
92 } 92 }
93 } 93 }
94 94
95 private byte[] encodeWithSystemDefaultFallback(String s) { 95 private byte[] encodeWithSystemDefaultFallback(CharSequence s) {
96 try { 96 try {
97 // synchronized(encoder) { 97 // synchronized(encoder) {
98 ByteBuffer bb = encoder.encode(CharBuffer.wrap(s)); 98 ByteBuffer bb = encoder.encode(CharBuffer.wrap(s));
99 // } 99 // }
100 byte[] rv = new byte[bb.remaining()]; 100 byte[] rv = new byte[bb.remaining()];
101 bb.get(rv, 0, rv.length); 101 bb.get(rv, 0, rv.length);
102 return rv; 102 return rv;
103 } catch (CharacterCodingException ex) { 103 } catch (CharacterCodingException ex) {
104 sessionContext.getLog().dump(getClass(), Error, ex, String.format("Use of charset %s failed, resort to system default", charset().name())); 104 sessionContext.getLog().dump(getClass(), Error, ex, String.format("Use of charset %s failed, resort to system default", charset().name()));
105 // resort to system-default 105 // resort to system-default
106 return s.getBytes(); 106 return s.toString().getBytes();
107 } 107 }
108 } 108 }
109 109
110 private Charset charset() { 110 private Charset charset() {
111 return encoder.charset(); 111 return encoder.charset();