@Test public void testLength() throws Exception { FileBackedClob c; c = new FileBackedClob(new CharArrayReader(testCase1)); assertEquals(c.length(), testCase1.length); assertEquals(c.getBackingFile().length(), testCase1.length * 4); c.free(); c = new FileBackedClob(new String(testCase2)); assertEquals(c.length(), testCase2.length); assertEquals(c.getBackingFile().length(), testCase2.length * 4); c.free(); c = new FileBackedClob(new CharArrayReader(testCase3)); assertEquals(c.length(), testCase3.length); assertEquals(c.getBackingFile().length(), testCase3.length * 4); c.free(); }
@Test public void testTruncate() throws Exception { FileBackedClob c; c = new FileBackedClob(new CharArrayReader(testCase1)); c.truncate(5); assertEquals(c.length(), 5); assertEquals(c.getBackingFile().length(), 5 * 4); c.free(); c = new FileBackedClob(new String(testCase2)); c.truncate(42); assertEquals(c.length(), 42); assertEquals(c.getBackingFile().length(), 42 * 4); c.free(); c = new FileBackedClob(new CharArrayReader(testCase3)); c.truncate(1024); assertEquals(c.length(), 1024); assertEquals(c.getBackingFile().length(), 1024 * 4); c.free(); }
@Test public void testGetSubString() throws Exception { FileBackedClob c; char[] referenceChars = new char[5]; System.arraycopy(testCase1, 5, referenceChars, 0, 5); String reference = new String(referenceChars); c = new FileBackedClob(new CharArrayReader(testCase1)); assertEquals(reference, c.getSubString(6, 5)); c.free(); c = new FileBackedClob(new String(testCase2)); assertEquals( reference, c.getSubString(6, 5)); c.free(); c = new FileBackedClob(new CharArrayReader(testCase3)); assertEquals(reference, c.getSubString(6, 5)); c.free(); }
@Test public void testSetString_long_String() throws Exception { FileBackedClob b; char[] test1 = "test".toCharArray(); char[] test2 = "test0123456789".toCharArray(); char[] firstPartReference = new char[testCase2.length - test1.length - 4]; char[] secondPartReference = new char[4]; System.arraycopy(testCase2, 0, firstPartReference, 0, testCase2.length - test1.length - 4); System.arraycopy(testCase2, testCase2.length - 4 - 1, secondPartReference, 0, 4); b = new FileBackedClob(new CharArrayReader(testCase2)); b.setString(testCase2.length - test1.length - 4 + 1, new String(test1)); assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4)); assertEquals(new String(secondPartReference), b.getSubString(testCase2.length - 4, 4)); assertEquals(new String(test1), b.getSubString(testCase2.length - 4 - test1.length + 1, test1.length)); assertEquals(b.length(), 1024); b.setString(testCase2.length - test1.length - 4 + 1, new String(test2)); assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4)); assertEquals(b.length(), 1024 - test1.length - 4 + test2.length); assertEquals(new String(test2), b.getSubString(b.length() - test2.length + 1, test2.length)); b.free(); }
@Test public void testSetString_4args() throws Exception { FileBackedClob b; char[] test1 = "test".toCharArray(); char[] test2 = "01test23456789".toCharArray(); char[] firstPartReference = new char[testCase2.length - test1.length - 4]; char[] secondPartReference = new char[4]; System.arraycopy(testCase2, 0, firstPartReference, 0, testCase2.length - test1.length - 4); System.arraycopy(testCase2, testCase2.length - 4 - 1, secondPartReference, 0, 4); b = new FileBackedClob(new CharArrayReader(testCase2)); b.setString(testCase2.length - test1.length - 4 + 1, new String(test2), 2, 4); assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4)); assertEquals(new String(secondPartReference), b.getSubString(testCase2.length - 4, 4)); assertEquals(new String(test1), b.getSubString(testCase2.length - 4 - test1.length + 1, test1.length)); assertEquals(b.length(), 1024); b.free(); }
@Test public void testSetCharacterStream() throws Exception { FileBackedClob b; char[] test1 = "test".toCharArray(); char[] test2 = "test0123456789".toCharArray(); char[] firstPartReference = new char[testCase2.length - test1.length - 4]; char[] secondPartReference = new char[4]; System.arraycopy(testCase2, 0, firstPartReference, 0, testCase2.length - test1.length - 4); System.arraycopy(testCase2, testCase2.length - 4 - 1, secondPartReference, 0, 4); b = new FileBackedClob(new CharArrayReader(testCase2)); Writer os = b.setCharacterStream(testCase2.length - test1.length - 4 + 1); os.write(test1); os.close(); assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4)); assertEquals(new String(secondPartReference), b.getSubString(testCase2.length - 4, 4)); assertEquals(new String(test1), b.getSubString(testCase2.length - 4 - test1.length + 1, test1.length)); assertEquals(b.length(), 1024); os = b.setCharacterStream(testCase2.length - test1.length - 4 + 1); os.write(test2); os.close(); assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4)); assertEquals(b.length(), 1024 - test1.length - 4 + test2.length); assertEquals(new String(test2), b.getSubString(b.length() - test2.length + 1, test2.length)); b.free(); }
@Override public boolean verifyUserSig(String identifier, String sig)throws QCloudException { try { Security.addProvider(new BouncyCastleProvider()); //DeBaseUrl64 urlSig to json Base64 decoder = new Base64(); byte [] compressBytes = Base64Url.base64DecodeUrl(sig.getBytes(Charset.forName("UTF-8"))); //Decompression Inflater decompression = new Inflater(); decompression.setInput(compressBytes, 0, compressBytes.length); byte [] decompressBytes = new byte [1024]; int decompressLength = decompression.inflate(decompressBytes); decompression.end(); String jsonString = new String(Arrays.copyOfRange(decompressBytes, 0, decompressLength)); //Get TLS.Sig from json JSONObject jsonObject= JSON.parseObject(jsonString); String sigTLS = jsonObject.getString("TLS.sig"); //debase64 TLS.Sig to get serailString byte[] signatureBytes = decoder.decode(sigTLS.getBytes(Charset.forName("UTF-8"))); String strSdkAppid = jsonObject.getString("TLS.sdk_appid"); String sigTime = jsonObject.getString("TLS.time"); String sigExpire = jsonObject.getString("TLS.expire_after"); if (!imConfig.getSdkAppId().equals(strSdkAppid)) { return false; } if ( System.currentTimeMillis()/1000 - Long.parseLong(sigTime) > Long.parseLong(sigExpire)) { return false; } //Get Serial String from json String SerialString = "TLS.appid_at_3rd:" + 0 + "\n" + "TLS.account_type:" + 0 + "\n" + "TLS.identifier:" + identifier + "\n" + "TLS.sdk_appid:" + imConfig.getSdkAppId() + "\n" + "TLS.time:" + sigTime + "\n" + "TLS.expire_after:" + sigExpire + "\n"; Reader reader = new CharArrayReader(imConfig.getPublicKey().toCharArray()); PEMParser parser = new PEMParser(reader); JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); Object obj = parser.readObject(); parser.close(); PublicKey pubKeyStruct = converter.getPublicKey((SubjectPublicKeyInfo) obj); Signature signature = Signature.getInstance("SHA256withECDSA","BC"); signature.initVerify(pubKeyStruct); signature.update(SerialString.getBytes(Charset.forName("UTF-8"))); return signature.verify(signatureBytes); }catch (Exception e) { throw new QCloudException(e); } }
public static void main(String[] args) throws Exception { char[] a = "_123456789_123456789_123456789_123456789" .toCharArray(); // a.length > 33 try (CharArrayReader car = new CharArrayReader(a)) { long small = 33; long big = Long.MAX_VALUE; long smallSkip = car.skip(small); if (smallSkip != small) throw new Exception("Expected to skip " + small + " chars, but skipped " + smallSkip); long expSkip = a.length - small; long bigSkip = car.skip(big); if (bigSkip != expSkip) throw new Exception("Expected to skip " + expSkip + " chars, but skipped " + bigSkip); } }
public static void main(String[] args) throws Exception { char[] a = "_123456789_123456789_123456789_123456789" .toCharArray(); // a.length > 33 try (CharArrayReader car = new CharArrayReader(a)) { int len1 = 33; char[] buf1 = new char[len1]; if (car.read(buf1, 0, len1) != len1) throw new Exception("Expected to read " + len1 + " chars"); int len2 = Integer.MAX_VALUE - 32; char[] buf2 = new char[len2]; int expLen2 = a.length - len1; if (car.read(buf2, 0, len2) != expLen2) throw new Exception("Expected to read " + expLen2 + " chars"); } }
public static void main(String[] args) throws Exception { int N = 10; for (int i = 0; i < N; i++) { HTMLEditorKit kit = new HTMLEditorKit(); Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator"); HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance(); HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument(); HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0); parser.parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true); htmlReader.flush(); CharArrayWriter writer = new CharArrayWriter(1000); kit.write(writer, doc, 0, doc.getLength()); writer.flush(); String result = writer.toString(); if (!result.contains("<tt><a")) { throw new RuntimeException("The <a> and <tt> tags are swapped"); } } }
public static void main(String[] args) { String htmlDoc = "<pre><p> </pre>"; try { HTMLEditorKit kit = new HTMLEditorKit(); Class c = Class.forName( "javax.swing.text.html.parser.ParserDelegator"); HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance(); HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument(); HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0); parser.parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true); htmlReader.flush(); CharArrayWriter writer = new CharArrayWriter(1000); kit.write(writer, doc, 0, doc.getLength()); writer.flush(); } catch (Exception ex) { throw new RuntimeException("Test Failed " + ex); } }
/** * Decode a string back to a byte array. * * @param s the string to convert * @param uncompress use gzip to uncompress the stream of bytes * * @throws IOException if there's a gzip exception */ public static byte[] decode(final String s, final boolean uncompress) throws IOException { byte[] bytes; try (JavaReader jr = new JavaReader(new CharArrayReader(s.toCharArray())); ByteArrayOutputStream bos = new ByteArrayOutputStream()) { int ch; while ((ch = jr.read()) >= 0) { bos.write(ch); } bytes = bos.toByteArray(); } if (uncompress) { final GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes)); final byte[] tmp = new byte[bytes.length * 3]; // Rough estimate int count = 0; int b; while ((b = gis.read()) >= 0) { tmp[count++] = (byte) b; } bytes = new byte[count]; System.arraycopy(tmp, 0, bytes, 0, count); } return bytes; }
@Test public void converterSelectedByControlRecord() throws Exception { String type1 = "type1"; String type2 = "type2"; String input = formatInput("!{0}\n" + NAME.code() + "\n^\n!{1}\n" + DATE.code() + "\n^\n", type1, type2); List<RecordConverter> converters = createConverterMocks(type1, type2); QifImport qifImport = new QifImport(converters); qifImport.importFile(new CharArrayReader(input.toCharArray()), messageConsumer); ArgumentCaptor<QifRecord> capture0 = ArgumentCaptor.forClass(QifRecord.class); ArgumentCaptor<QifRecord> capture1 = ArgumentCaptor.forClass(QifRecord.class); verify(converters.get(0)).importRecord(isA(AccountHolder.class), capture0.capture()); verify(converters.get(1)).importRecord(isA(AccountHolder.class), capture1.capture()); assertThat(capture0.getValue().hasValue(NAME)).isTrue(); assertThat(capture0.getValue().hasValue(DATE)).isFalse(); assertThat(capture1.getValue().hasValue(DATE)).isTrue(); assertThat(capture1.getValue().hasValue(NAME)).isFalse(); }
@Test public void exceptionThrownForInvalidRecord() throws Exception { String type = "type"; String input = formatInput("!{0}\n" + NAME.code() + "\n!\n", type); List<RecordConverter> converters = createConverterMocks(type); QifImport qifImport = new QifImport(converters); try { qifImport.importFile(new CharArrayReader(input.toCharArray()), messageConsumer); fail("expected an exception"); } catch (QuickenException ex) { assertThat(ex.getMessageKey()).isEqualTo("import.qif.invalidRecord"); assertThat(ex.getMessageArgs()).isEqualTo(new Object[] {"QIF", 2L}); } }
@Test public void quickenExceptionsNotCaught() throws Exception { String type = "type"; String input = formatInput("!{0}\n" + NAME.code() + "\n^\n^\n" + DATE.code() + "\n^\n" + DATE.code() + "\n^\n", type); List<RecordConverter> converters = createConverterMocks(type); RecordConverter converter = converters.get(0); QuickenException cause = new QuickenException("key", "one", 1L); doThrow(cause).when(converter).importRecord(isA(AccountHolder.class), isA(QifRecord.class)); QifImport qifImport = new QifImport(converters); try { qifImport.importFile(new CharArrayReader(input.toCharArray()), messageConsumer); fail("expected an exception"); } catch (QuickenException ex) { assertThat(ex).isSameAs(cause); } }
public static void baseIO() throws Exception { // 字节流和字符流顶层接口: // 字节流:InputStream,OutputStream // 字符流:Reader,Writer // 节点流分析 // 字节流:FileInputStream,ByteArrayInputStream // 字符流:FileReader,CharArrayInputStream FileInputStream ifs = new FileInputStream(TEST_FILE); FileOutputStream iof = new FileOutputStream(TEST_FILE); ByteArrayInputStream bais = new ByteArrayInputStream(new byte[1024]); FileReader fr = new FileReader(TEST_FILE); CharArrayReader car = new CharArrayReader(new char[1024]); // 处理流分析 // 字节流:BufferedInputStream // 字符流:BufferedReader,InputStreamReader BufferedInputStream bis = new BufferedInputStream(ifs); BufferedReader br = new BufferedReader(fr); InputStreamReader isr = new InputStreamReader(ifs);// 将字节流转换为字符流 }
/** * java.io.CharArrayReader#mark(int) */ public void test_markI() throws IOException { cr = new CharArrayReader(hw); cr.skip(5L); cr.mark(100); cr.read(); cr.reset(); assertEquals("Test 1: Failed to mark correct position;", 'W', cr.read()); cr.close(); try { cr.mark(100); fail("Test 2: IOException expected."); } catch (IOException e) { // Expected. } }
/** * java.io.CharArrayReader#reset() */ public void test_reset() throws IOException { cr = new CharArrayReader(hw); cr.skip(5L); cr.mark(100); cr.read(); cr.reset(); assertEquals("Test 1: Reset failed to return to marker position.", 'W', cr.read()); cr.close(); try { cr.reset(); fail("Test 2: IOException expected."); } catch (IOException e) { // Expected. } }
public void test_skipJ() throws IOException { long skipped = 0; cr = new CharArrayReader(hw); skipped = cr.skip(5L); assertEquals("Test 1: Failed to skip correct number of chars;", 5L, skipped); assertEquals("Test 2: Skip skipped wrong chars;", 'W', cr.read()); cr.close(); try { cr.skip(1); fail("Test 3: IOException expected."); } catch (IOException e) { // Expected. } }
@Override public void write(Writer writer, SolrQueryRequest request, SolrQueryResponse response) throws IOException { final Transformer t = getTransformer(request); // capture the output of the XMLWriter final CharArrayWriter w = new CharArrayWriter(); XMLWriter.writeResponse(w,request,response); // and write transformed result to our writer final Reader r = new BufferedReader(new CharArrayReader(w.toCharArray())); final StreamSource source = new StreamSource(r); final StreamResult result = new StreamResult(writer); try { t.transform(source, result); } catch(TransformerException te) { throw new IOException("XSLT transformation error", te); } }
@Override public void write(char[] cbuf, int off, int len) throws IOException { BufferedReader sr=new BufferedReader(new CharArrayReader(cbuf,off,len)); String s; while((s=sr.readLine())!=null){ Matcher m=pattern.matcher(s); if(m.matches()){ int n=m.groupCount(); if(n==0){ sqls.add(new String[]{m.group()}); }else{ String[] result=new String[n]; for(int i=1;i<=n;i++){ result[i-1]=m.group(i); } sqls.add(result); } } } }
/** * @tests java.io.StreamTokenizer#slashSlashComments(boolean) */ @TestTargetNew( level = TestLevel.PARTIAL, notes = "", method = "slashSlashComments", args = {boolean.class} ) public void test_slashSlashComments_withSSOpen() throws IOException { Reader reader = new CharArrayReader( "t // t t t".toCharArray()); StreamTokenizer st = new StreamTokenizer(reader); st.slashSlashComments(true); assertEquals(StreamTokenizer.TT_WORD,st.nextToken()); assertEquals(StreamTokenizer.TT_EOF,st.nextToken()); }
/** * @tests java.io.StreamTokenizer#slashSlashComments(boolean) */ @TestTargetNew( level = TestLevel.PARTIAL, notes = "", method = "slashSlashComments", args = {boolean.class} ) public void test_slashSlashComments_withSSClosed() throws IOException { Reader reader = new CharArrayReader( "// t".toCharArray()); StreamTokenizer st = new StreamTokenizer(reader); st.slashSlashComments(false); st.ordinaryChar('/'); assertEquals('/',st.nextToken()); assertEquals('/',st.nextToken()); assertEquals(StreamTokenizer.TT_WORD,st.nextToken()); }
/** * @tests java.io.StreamTokenizer#slashStarComments(boolean) */ @TestTargetNew( level = TestLevel.PARTIAL, notes = "", method = "slashStarComments", args = {boolean.class} ) public void test_slashStarComments_withSTOpen() throws IOException { Reader reader = new CharArrayReader( "t /* t */ t".toCharArray()); StreamTokenizer st = new StreamTokenizer(reader); st.slashStarComments(true); assertEquals(StreamTokenizer.TT_WORD,st.nextToken()); assertEquals(StreamTokenizer.TT_WORD,st.nextToken()); assertEquals(StreamTokenizer.TT_EOF,st.nextToken()); }
/** * @tests java.io.CharArrayWriter#reset() */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Verifies reset() method.", method = "reset", args = {} ) public void test_reset() { // Test for method void java.io.CharArrayWriter.reset() cw.write("HelloWorld", 5, 5); cw.reset(); cw.write("HelloWorld", 0, 5); cr = new CharArrayReader(cw.toCharArray()); try { char[] c = new char[100]; cr.read(c, 0, 5); assertEquals("Reset failed to reset buffer", "Hello", new String(c, 0, 5)); } catch (IOException e) { fail("Exception during reset test : " + e.getMessage()); } }
/** * @tests java.io.CharArrayWriter#toCharArray() */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Verifies toCharArray() method.", method = "toCharArray", args = {} ) public void test_toCharArray() { // Test for method char [] java.io.CharArrayWriter.toCharArray() cw.write("HelloWorld", 0, 10); cr = new CharArrayReader(cw.toCharArray()); try { char[] c = new char[100]; cr.read(c, 0, 10); assertEquals("toCharArray failed to return correct array", "HelloWorld", new String(c, 0, 10)); } catch (IOException e) { fail("Exception during toCharArray test : " + e.getMessage()); } }
/** * @tests java.io.CharArrayWriter#write(char[], int, int) */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, method = "write", args = {char[].class, int.class, int.class} ) public void test_write$CII() { // Test for method void java.io.CharArrayWriter.write(char [], int, int) cw.write(hw, 5, 5); cr = new CharArrayReader(cw.toCharArray()); try { char[] c = new char[100]; cr.read(c, 0, 5); assertEquals("Writer failed to write correct chars", "World", new String(c, 0, 5)); } catch (IOException e) { fail("Exception during write test : " + e.getMessage()); } }
/** * @tests java.io.CharArrayWriter#write(int) */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Verifies write(int) method.", method = "write", args = {int.class} ) public void test_writeI() { // Test for method void java.io.CharArrayWriter.write(int) cw.write('T'); cr = new CharArrayReader(cw.toCharArray()); try { assertEquals("Writer failed to write char", 'T', cr.read()); } catch (IOException e) { fail("Exception during write test : " + e.getMessage()); } }
/** * @tests java.io.CharArrayWriter#write(java.lang.String, int, int) */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Verifies write(java.lang.String, int, int) method. [Need to check different strings?]", method = "write", args = {java.lang.String.class, int.class, int.class} ) public void test_writeLjava_lang_StringII() { // Test for method void java.io.CharArrayWriter.write(java.lang.String, // int, int) cw.write("HelloWorld", 5, 5); cr = new CharArrayReader(cw.toCharArray()); try { char[] c = new char[100]; cr.read(c, 0, 5); assertEquals("Writer failed to write correct chars", "World", new String(c, 0, 5)); } catch (IOException e) { fail("Exception during write test : " + e.getMessage()); } }