private String getSubstringByte(final Object obj, final int capacity) throws CharacterCodingException,
			UnsupportedEncodingException {

		String str = obj == null ? "null" : obj.toString();
		if (capacity < 1) {
			return str;
		}

		CharsetEncoder ce = Charset.forName(ENCODING_SHIFT_JIS).newEncoder()
				.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).reset();
		if (capacity >= ce.maxBytesPerChar() * str.length()) {
			return str;
		}
		CharBuffer cb = CharBuffer.wrap(new char[Math.min(str.length(), capacity)]);
		str.getChars(0, Math.min(str.length(), cb.length()), cb.array(), 0);

		if (capacity >= ce.maxBytesPerChar() * cb.limit()) {
			return cb.toString();
		}
		ByteBuffer out = ByteBuffer.allocate(capacity);
		ce.reset();
		CoderResult cr = null;
		if (cb.hasRemaining()) {
			cr = ce.encode(cb, out, true);
		} else {
			cr = CoderResult.UNDERFLOW;
		}
		if (cr.isUnderflow()) {
			cr = ce.flush(out);
		}
		return cb.flip().toString();
	}
private String getSubstringByte ( final Object obj , final int capacity ) throws CharacterCodingException , UnsupportedEncodingException { String str = obj == null ? "null" : obj . toString ( ) ; if ( capacity < 1 ) { return str ; } CharsetEncoder ce = Charset . forName ( ENCODING_SHIFT_JIS ) . newEncoder ( ) . onMalformedInput ( CodingErrorAction . REPLACE ) . onUnmappableCharacter ( CodingErrorAction . REPLACE ) . reset ( ) ; if ( capacity >= ce . maxBytesPerChar ( ) * str . length ( ) ) { return str ; } CharBuffer cb = CharBuffer . wrap ( new char [ Math . min ( str . length ( ) , capacity ) ] ) ; str . getChars ( 0 , Math . min ( str . length ( ) , cb . length ( ) ) , cb . array ( ) , 0 ) ; if ( capacity >= ce . maxBytesPerChar ( ) * cb . limit ( ) ) { return cb . toString ( ) ; } ByteBuffer out = ByteBuffer . allocate ( capacity ) ; ce . reset ( ) ; CoderResult cr = null ; if ( cb . hasRemaining ( ) ) { cr = ce . encode ( cb , out , true ) ; } else { cr = CoderResult . UNDERFLOW ; } if ( cr . isUnderflow ( ) ) { cr = ce . flush ( out ) ; } return cb . flip ( ) . toString ( ) ; }
指定したバイト数で文字列をカットする

@param obj 対象オブジェクト
@param capacity カットするバイト数
@return String
@throws CharacterCodingException
@throws UnsupportedEncodingException