Package org.nfctools.ndef.wkt.records

Examples of org.nfctools.ndef.wkt.records.TextRecord


    assertEquals("826465005400650073007400F600E400FC00DF00D600C400DC003F", NfcUtils.convertBinToASCII(bytes));
  }

  @Test
  public void testCreateTextRecordUtf8English() throws Exception {
    TextRecord textRecord = new TextRecord(string, Charset.forName("utf8"), Locale.ENGLISH);
    byte[] bytes = encoder.encodePayload(textRecord, messageEncoder);
    assertEquals("02656E54657374C3B6C3A4C3BCC39FC396C384C39C3F", NfcUtils.convertBinToASCII(bytes));
  }
View Full Code Here


    assertEquals("02656E54657374C3B6C3A4C3BCC39FC396C384C39C3F", NfcUtils.convertBinToASCII(bytes));
  }

  @Test
  public void testCreateTextRecordUtf16English() throws Exception {
    TextRecord textRecord = new TextRecord(string, Charset.forName("utf-16be"), Locale.ENGLISH);
    byte[] bytes = encoder.encodePayload(textRecord, messageEncoder);
    assertEquals("82656E005400650073007400F600E400FC00DF00D600C400DC003F", NfcUtils.convertBinToASCII(bytes));
  }
View Full Code Here

    assertEquals("file://localhost/Appli/CustomerBonus", uriRecord.getUri());

    assertTrue(gcr.getAction().hasActionRecord());
    assertTrue(gcr.getAction().getActionRecord() instanceof TextRecord);

    TextRecord actionTextRecord = (TextRecord)gcr.getAction().getActionRecord();

    assertEquals("add", actionTextRecord.getText());

    assertEquals(1, gcr.getData().getRecords().size());
  }
View Full Code Here

    encoder.encode(records);
  }

  @Test
  public void testEncodeMultiple() throws Exception {
    byte[] encode = encoder.encode(new TextRecord("R1", Locale.GERMANY), new TextRecord("R2", Locale.GERMANY));
    assertEquals("910108540564652D44455231510108540564652D44455232", NfcUtils.convertBinToASCII(encode));
  }
View Full Code Here

public class TextRecordTest {

  @Test(expected = IllegalArgumentException.class)
  public void testUnsupportedEncodingUsAscii() throws Exception {

    new TextRecord("", Charset.forName("US-ASCII"), Locale.GERMAN);
  }
View Full Code Here

  }

  @Test(expected = IllegalArgumentException.class)
  public void testUnsupportedEncodingUtf16() throws Exception {

    new TextRecord("", Charset.forName("utf16"), Locale.GERMAN);
  }
View Full Code Here

      + "303F626F64793D486921253230576965253230676568742532306573253230646972253346";

  @Test
  public void testEncode() throws Exception {
    SmartPosterRecord smartPosterRecord = new SmartPosterRecord();
    smartPosterRecord.setTitle(new TextRecord("Test", Charset.forName("UTF8"), Locale.GERMAN));
    smartPosterRecord.setUri(new UriRecord("sms:+491234567890?body=Hi!%20Wie%20geht%20es%20dir%3F"));
    byte[] payload = messageEncoder.encodeSingle(smartPosterRecord);
    assertEquals(innerSmartPoster, NfcUtils.convertBinToASCII(payload));
  }
View Full Code Here

  }

  @Test
  public void testEncode2() throws Exception {
    SmartPosterRecord smartPosterRecord = new SmartPosterRecord();
    smartPosterRecord.setTitle(new TextRecord("Title", Charset.forName("UTF8"), Locale.GERMANY));
    smartPosterRecord.setUri(new UriRecord("http://www.winfuture.de"));
    smartPosterRecord.setAction(new ActionRecord(Action.DEFAULT_ACTION));
    byte[] payload = messageEncoder.encodeSingle(smartPosterRecord);
    assertEquals(SmartPosterDecoderTest.smartPoster, NfcUtils.convertBinToASCII(payload));
  }
View Full Code Here

public class TextRecordEncoder implements WellKnownRecordPayloadEncoder {

  @Override
  public byte[] encodePayload(WellKnownRecord wellKnownRecord, NdefMessageEncoder messageEncoder) {

    TextRecord textRecord = (TextRecord)wellKnownRecord;

    if(!textRecord.hasLocale()) {
      throw new NdefEncoderException("Expected locale", wellKnownRecord);
    }

    if(!textRecord.hasEncoding()) {
      throw new NdefEncoderException("Expected encoding", wellKnownRecord);
    }

    if(!textRecord.hasText()) {
      throw new NdefEncoderException("Expected text", wellKnownRecord);
    }

    Locale locale = textRecord.getLocale();

    byte[] languageData = (locale.getLanguage() + (locale.getCountry() == null || locale.getCountry().length() == 0 ? ""
        : ("-" + locale.getCountry()))).getBytes();

    if (languageData.length > TextRecord.LANGUAGE_CODE_MASK) {
      throw new NdefEncoderException("language code length longer than 2^5. this is not supported.", wellKnownRecord);
    }
   
    Charset encoding = textRecord.getEncoding();

    byte[] textData = getTextAsBytes(textRecord, encoding);
    byte[] payload = new byte[1 + languageData.length + textData.length];

    byte status = (byte)(languageData.length | (TextRecord.UTF16.equals(encoding) ? 0x80 : 0x00));
View Full Code Here

    byte[] textData = RecordUtils.getBytesFromStream(payload.length - languageCodeLength - 1, bais);
    Charset textEncoding = ((status & 0x80) != 0) ? TextRecord.UTF16 : TextRecord.UTF8;

    try {
      String text = new String(textData, textEncoding.name());
      return new TextRecord(text, textEncoding, new Locale(languageCode));
    }
    catch (UnsupportedEncodingException e) {
      throw new RuntimeException(e);
    }
  }
View Full Code Here

TOP

Related Classes of org.nfctools.ndef.wkt.records.TextRecord

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.