Example usage for java.io DataInputStream readUnsignedByte

List of usage examples for java.io DataInputStream readUnsignedByte

Introduction

In this page you can find the example usage for java.io DataInputStream readUnsignedByte.

Prototype

public final int readUnsignedByte() throws IOException 

Source Link

Document

See the general contract of the readUnsignedByte method of DataInput.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileInputStream fin = new FileInputStream("C:/UnsignedByte.txt");
    DataInputStream din = new DataInputStream(fin);

    int i = din.readUnsignedByte();
    System.out.println("Unsinged byte value : " + i);
    din.close();//from w  w w.j a  va 2s.c o  m
}

From source file:Main.java

public static void main(String[] args) throws IOException {

    byte[] b = { -123, 4 };

    FileOutputStream fos = new FileOutputStream("c:\\test.txt");
    DataOutputStream dos = new DataOutputStream(fos);

    for (byte j : b) {
        dos.writeByte(j);//  w w  w .ja  v  a 2 s  .co m
    }
    dos.flush();
    InputStream is = new FileInputStream("c:\\test.txt");
    DataInputStream dis = new DataInputStream(is);

    while (dis.available() > 0) {
        int k = dis.readUnsignedByte();
        System.out.print(k);
    }

}

From source file:cn.xiongyihui.wificar.MjpegStream.java

private int getEndOfSeqeunce(DataInputStream in, byte[] sequence) throws IOException {
    int seqIndex = 0;
    byte c;//from   w  ww. j  a v  a 2s. com
    for (int i = 0; i < FRAME_MAX_LENGTH; i++) {
        c = (byte) in.readUnsignedByte();
        if (c == sequence[seqIndex]) {
            seqIndex++;
            if (seqIndex == sequence.length)
                return i + 1;
        } else
            seqIndex = 0;
    }
    return -1;
}

From source file:net.minecraftforge.fml.repackage.com.nothome.delta.GDiffPatcher.java

/**
 * Patches to an output stream./*from   w ww .j  av  a 2s. c o m*/
 */
public void patch(SeekableSource source, InputStream patch, OutputStream out) throws IOException {

    DataOutputStream outOS = new DataOutputStream(out);
    DataInputStream patchIS = new DataInputStream(patch);

    // the magic string is 'd1 ff d1 ff' + the version number
    if (patchIS.readUnsignedByte() != 0xd1 || patchIS.readUnsignedByte() != 0xff
            || patchIS.readUnsignedByte() != 0xd1 || patchIS.readUnsignedByte() != 0xff
            || patchIS.readUnsignedByte() != 0x04) {

        throw new PatchException("magic string not found, aborting!");
    }

    while (true) {
        int command = patchIS.readUnsignedByte();
        if (command == EOF)
            break;
        int length;
        int offset;

        if (command <= DATA_MAX) {
            append(command, patchIS, outOS);
            continue;
        }

        switch (command) {
        case DATA_USHORT: // ushort, n bytes following; append
            length = patchIS.readUnsignedShort();
            append(length, patchIS, outOS);
            break;
        case DATA_INT: // int, n bytes following; append
            length = patchIS.readInt();
            append(length, patchIS, outOS);
            break;
        case COPY_USHORT_UBYTE:
            offset = patchIS.readUnsignedShort();
            length = patchIS.readUnsignedByte();
            copy(offset, length, source, outOS);
            break;
        case COPY_USHORT_USHORT:
            offset = patchIS.readUnsignedShort();
            length = patchIS.readUnsignedShort();
            copy(offset, length, source, outOS);
            break;
        case COPY_USHORT_INT:
            offset = patchIS.readUnsignedShort();
            length = patchIS.readInt();
            copy(offset, length, source, outOS);
            break;
        case COPY_INT_UBYTE:
            offset = patchIS.readInt();
            length = patchIS.readUnsignedByte();
            copy(offset, length, source, outOS);
            break;
        case COPY_INT_USHORT:
            offset = patchIS.readInt();
            length = patchIS.readUnsignedShort();
            copy(offset, length, source, outOS);
            break;
        case COPY_INT_INT:
            offset = patchIS.readInt();
            length = patchIS.readInt();
            copy(offset, length, source, outOS);
            break;
        case COPY_LONG_INT:
            long loffset = patchIS.readLong();
            length = patchIS.readInt();
            copy(loffset, length, source, outOS);
            break;
        default:
            throw new IllegalStateException("command " + command);
        }
    }
    outOS.flush();
}

From source file:fr.noop.subtitle.stl.StlParser.java

private SubtitleTimeCode readTimeCode(DataInputStream dis, int frameRate)
        throws IOException, InvalidTimeRangeException {
    int hour = dis.readUnsignedByte();
    int minute = dis.readUnsignedByte();
    int second = dis.readUnsignedByte();
    int frame = dis.readUnsignedByte();

    // Frame duration in milliseconds
    int frameDuration = (1000 / frameRate);

    // Build time code
    return new SubtitleTimeCode(hour, minute, second, frame * frameDuration);
}

From source file:de.hybris.platform.cuppytrail.impl.DefaultSecureTokenService.java

private void skipPadding(final DataInputStream dataInputStream) throws IOException {
    final int firstByte = dataInputStream.readUnsignedByte();

    // Find number of additional bytes to skip (stored in the bottom 3 bits)
    final int length = firstByte & 0x0007;

    for (int i = 0; i < length; i++) {
        dataInputStream.readByte();//from  w  w w  . j a  v a 2s . co m
    }
}

From source file:com.envirover.spl.SPLGroungControlTest.java

@Test
public void testMOMessagePipeline()
        throws URISyntaxException, ClientProtocolException, IOException, InterruptedException {
    System.out.println("MO TEST: Testing MO message pipeline...");

    Thread.sleep(1000);/*from  w w w .  j a v a2 s . c o m*/

    Thread mavlinkThread = new Thread(new Runnable() {
        public void run() {
            Socket client = null;

            try {
                System.out.printf("MO TEST: Connecting to tcp://%s:%d",
                        InetAddress.getLocalHost().getHostAddress(), config.getMAVLinkPort());
                System.out.println();

                client = new Socket(InetAddress.getLocalHost().getHostAddress(), config.getMAVLinkPort());

                System.out.printf("MO TEST: Connected tcp://%s:%d", InetAddress.getLocalHost().getHostAddress(),
                        config.getMAVLinkPort());
                System.out.println();

                Parser parser = new Parser();
                DataInputStream in = new DataInputStream(client.getInputStream());
                while (true) {
                    MAVLinkPacket packet;
                    do {
                        int c = in.readUnsignedByte();
                        packet = parser.mavlink_parse_char(c);
                    } while (packet == null);

                    System.out.printf("MO TEST: MAVLink message received: msgid = %d", packet.msgid);
                    System.out.println();

                    Thread.sleep(100);
                }
            } catch (InterruptedException ex) {
                return;
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    mavlinkThread.start();

    HttpClient httpclient = HttpClients.createDefault();

    URIBuilder builder = new URIBuilder();
    builder.setScheme("http");
    builder.setHost(InetAddress.getLocalHost().getHostAddress());
    builder.setPort(config.getRockblockPort());
    builder.setPath(config.getHttpContext());

    URI uri = builder.build();
    HttpPost httppost = new HttpPost(uri);

    // Request parameters and other properties.
    List<NameValuePair> params = new ArrayList<NameValuePair>(2);
    params.add(new BasicNameValuePair("imei", config.getRockBlockIMEI()));
    params.add(new BasicNameValuePair("momsn", "12345"));
    params.add(new BasicNameValuePair("transmit_time", "12-10-10 10:41:50"));
    params.add(new BasicNameValuePair("iridium_latitude", "52.3867"));
    params.add(new BasicNameValuePair("iridium_longitude", "0.2938"));
    params.add(new BasicNameValuePair("iridium_cep", "9"));
    params.add(new BasicNameValuePair("data", Hex.encodeHexString(getSamplePacket().encodePacket())));
    httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

    // Execute and get the response.
    System.out.printf("MO TEST: Sending test message to %s", uri.toString());
    System.out.println();

    HttpResponse response = httpclient.execute(httppost);

    if (response.getStatusLine().getStatusCode() != 200) {
        fail(String.format("RockBLOCK HTTP message handler status code = %d.",
                response.getStatusLine().getStatusCode()));
    }

    HttpEntity entity = response.getEntity();

    if (entity != null) {
        InputStream responseStream = entity.getContent();
        try {
            String responseString = IOUtils.toString(responseStream);
            System.out.println(responseString);
        } finally {
            responseStream.close();
        }
    }

    Thread.sleep(1000);

    mavlinkThread.interrupt();
    System.out.println("MO TEST: Complete.");
}

From source file:fr.noop.subtitle.stl.StlParser.java

private StlTti readTti(DataInputStream dis, StlGsi gsi) throws IOException, InvalidTimeRangeException {
    // Get charset from gsi
    String charset = gsi.getCct().getCharset();

    // Get frame rate from gsi
    int frameRate = gsi.getDfc().getFrameRate();

    // Read and extract metadata from TTI block
    // Each TTI block is 128 bytes long
    StlTti tti = new StlTti();

    // Read Subtitle Group Number (SGN)
    tti.setSgn((short) dis.readUnsignedByte());

    // Read Subtitle Number (SN)
    tti.setSn(Short.reverseBytes(dis.readShort()));

    // Read Extension Block Number (EBN)
    tti.setEbn((short) dis.readUnsignedByte());

    // Read Cumulative Status (CS)
    tti.setCs((short) dis.readUnsignedByte());

    // Read Time Code In (TCI)
    tti.setTci(this.readTimeCode(dis, frameRate));

    // Read Time Code Out (TCO)
    tti.setTco(this.readTimeCode(dis, frameRate));

    // Read Vertical Position (VP)
    tti.setVp((short) dis.readUnsignedByte());

    // Read Justification Code (JC)
    tti.setJc(StlTti.Jc.getEnum(dis.readUnsignedByte()));

    // Read Comment Flag (CF)
    tti.setCf((short) dis.readUnsignedByte());

    // Read TextField (TF)
    byte[] tfBytes = new byte[112];
    dis.readFully(tfBytes, 0, 112);//from   w w  w. j  a v a2  s .  co  m
    tti.setTf(new String(tfBytes, charset));

    // TTI is fully parsed
    return tti;
}

From source file:fr.noop.subtitle.stl.StlParser.java

private StlGsi readGsi(DataInputStream dis) throws IOException, InvalidTimeRangeException {
    // Read and extract metadata from GSI block
    // GSI block is 1024 bytes long
    StlGsi gsi = new StlGsi();

    // Read Code Page Number (CPN)
    byte[] cpnBytes = new byte[3];
    dis.readFully(cpnBytes, 0, 3);//www  . j  a v a 2  s . c om
    int cpn = cpnBytes[0] << 16 | cpnBytes[1] << 8 | cpnBytes[2];
    gsi.setCpn(StlGsi.Cpn.getEnum(cpn));

    // Read Disk Format Code (DFC)
    gsi.setDfc(StlGsi.Dfc.getEnum(this.readString(dis, 8)));

    // Read Display Standard Code (DSC)
    gsi.setDsc(StlGsi.Dsc.getEnum(dis.readUnsignedByte()));

    // Read Character Code Table number (CCT)
    gsi.setCct(StlGsi.Cct.getEnum(Short.reverseBytes(dis.readShort())));

    // Read Character Language Code (LC)
    gsi.setLc(Short.reverseBytes(dis.readShort()));

    // Read Original Programme Title (OPT)
    gsi.setOpt(this.readString(dis, 32));

    // Read Original Programme Title (OET)
    gsi.setOet(this.readString(dis, 32));

    // Read Translated Programme Title (TPT)
    gsi.setTpt(this.readString(dis, 32));

    // Read translated Episode Title (TET)
    gsi.setTet(this.readString(dis, 32));

    // Read Translator's Name (TN)
    gsi.setTn(this.readString(dis, 32));

    // Read Translator's Contact Details (TCD)
    gsi.setTcd(this.readString(dis, 32));

    // Read Subtitle List Reference Code (SLR)
    gsi.setSlr(this.readString(dis, 16));

    // Read Creation Date (CD)
    gsi.setCd(this.readDate(this.readString(dis, 6)));

    // Read Revision Date (RD)
    gsi.setRd(this.readDate(this.readString(dis, 6)));

    // Read Revision number RN
    gsi.setRn(Short.reverseBytes(dis.readShort()));

    // Read Total Number of Text and Timing Information (TTI) blocks (TNB)
    gsi.setTnb(Integer.parseInt(this.readString(dis, 5)));

    // Read Total Number of Subtitles (TNS)
    gsi.setTns(Integer.parseInt(this.readString(dis, 5)));

    // Read Total Number of Subtitle Groups (TNG)
    dis.skipBytes(3);

    // Read Maximum Number of Displayable Characters in any text row (MNC)
    gsi.setMnc(Integer.parseInt(this.readString(dis, 2)));

    // Read Maximum Number of Displayable Rows (MNR)
    gsi.setMnr(Integer.parseInt(this.readString(dis, 2)));

    // Read Time Code: Status (TCS)
    gsi.setTcs((short) dis.readUnsignedByte());

    // Read Time Code: Start-of-Programme (TCP)
    gsi.setTcp(this.readTimeCode(this.readString(dis, 8), gsi.getDfc().getFrameRate()));

    // Read Time Code: First In-Cue (TCF)
    gsi.setTcf(this.readTimeCode(this.readString(dis, 8), gsi.getDfc().getFrameRate()));

    // Read Total Number of Disks (TND)
    gsi.setTnd((short) dis.readUnsignedByte());

    // Read Disk Sequence Number (DSN)
    gsi.setDsn((short) dis.readUnsignedByte());

    // Read Country of Origin (CO)
    gsi.setCo(this.readString(dis, 3));

    // Read Publisher (PUB)
    gsi.setPub(this.readString(dis, 32));

    // Read Editor's Name (EN)
    gsi.setEn(this.readString(dis, 32));

    // Read Editor's Contact Details (ECD)
    gsi.setEcd(this.readString(dis, 32));

    // Spare Bytes
    dis.skipBytes(75);

    // Read User-Defined Area (UDA)
    gsi.setUda(this.readString(dis, 576));

    return gsi;
}

From source file:com.ning.arecibo.util.timeline.times.TimelineCoderImpl.java

@Override
public List<DateTime> decompressDateTimes(final byte[] compressedTimes) {
    final List<DateTime> dateTimeList = new ArrayList<DateTime>(compressedTimes.length * 4);
    final ByteArrayInputStream byteStream = new ByteArrayInputStream(compressedTimes);
    final DataInputStream byteDataStream = new DataInputStream(byteStream);
    int opcode = 0;
    int lastTime = 0;
    try {//from  w w w. j  a  v a2s.  c om
        while (true) {
            opcode = byteDataStream.read();
            if (opcode == -1) {
                break;
            }

            if (opcode == TimelineOpcode.FULL_TIME.getOpcodeIndex()) {
                lastTime = byteDataStream.readInt();
                dateTimeList.add(DateTimeUtils.dateTimeFromUnixSeconds(lastTime));
            } else if (opcode == TimelineOpcode.REPEATED_DELTA_TIME_BYTE.getOpcodeIndex()) {
                final int repeatCount = byteDataStream.readUnsignedByte();
                final int delta = byteDataStream.readUnsignedByte();
                for (int i = 0; i < repeatCount; i++) {
                    lastTime = lastTime + delta;
                    dateTimeList.add(DateTimeUtils.dateTimeFromUnixSeconds(lastTime));
                }
            } else if (opcode == TimelineOpcode.REPEATED_DELTA_TIME_SHORT.getOpcodeIndex()) {
                final int repeatCount = byteDataStream.readUnsignedShort();
                final int delta = byteDataStream.readUnsignedByte();
                for (int i = 0; i < repeatCount; i++) {
                    lastTime = lastTime + delta;
                    dateTimeList.add(DateTimeUtils.dateTimeFromUnixSeconds(lastTime));
                }
            } else {
                // The opcode is itself a singleton delta
                lastTime = lastTime + opcode;
                dateTimeList.add(DateTimeUtils.dateTimeFromUnixSeconds(lastTime));
            }
        }
    } catch (IOException e) {
        log.error(e, "In decompressTimes(), exception decompressing");
    }
    return dateTimeList;
}