Example usage for java.io DataInputStream DataInputStream

List of usage examples for java.io DataInputStream DataInputStream

Introduction

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

Prototype

public DataInputStream(InputStream in) 

Source Link

Document

Creates a DataInputStream that uses the specified underlying InputStream.

Usage

From source file:HttpGETMIDlet.java

private String requestUsingGET(String URLString) throws IOException {
    HttpConnection hpc = null;//from  w  ww .  jav  a  2  s .c o  m
    DataInputStream dis = null;
    boolean newline = false;
    String content = "";
    try {
        hpc = (HttpConnection) Connector.open(URLString);
        dis = new DataInputStream(hpc.openInputStream());
        int character;

        while ((character = dis.read()) != -1) {
            if ((char) character == '\\') {
                newline = true;
                continue;
            } else {
                if ((char) character == 'n' && newline) {
                    content += "\n";
                    newline = false;
                } else if (newline) {
                    content += "\\" + (char) character;
                    newline = false;
                } else {
                    content += (char) character;
                    newline = false;
                }
            }

        }
        if (hpc != null)
            hpc.close();
        if (dis != null)
            dis.close();
    } catch (IOException e2) {
    }

    return content;
}

From source file:eu.learnpad.simulator.mon.utils.Manager.java

/**
 * It reads text from file and provides it on string
 *     * /*from  w  w w .  j  a v  a 2  s. c  om*/
 * @param filePath the file to read path
 * @return a String containing all the file text
 */
@SuppressWarnings("deprecation")
public static String ReadTextFromFile(String filePath) {
    File file = new File(filePath);
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;
    StringBuilder strB = new StringBuilder();

    try {
        fis = new FileInputStream(file);

        // Here BufferedInputStream is added for fast reading.
        bis = new BufferedInputStream(fis);
        dis = new DataInputStream(bis);

        while (dis.available() != 0) {
            // this statement reads the line from the file and print it to
            // the console.
            strB.append(dis.readLine());

        }
        // dispose all the resources after using them.
        fis.close();
        bis.close();
        dis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return strB.toString();
}

From source file:com.mozilla.hadoop.hbase.mapreduce.MultiScanTableMapReduceUtil.java

/**
 * Converts base64 scans string back into a Scan array
 * @param base64/* w  w  w.j  a v  a 2  s  .  c o m*/
 * @return
 * @throws IOException
 */
public static Scan[] convertStringToScanArray(final String base64) throws IOException {
    final DataInputStream dis = new DataInputStream(new ByteArrayInputStream(Base64.decode(base64)));

    ArrayWritable aw = new ArrayWritable(Scan.class);
    aw.readFields(dis);

    Writable[] writables = aw.get();
    Scan[] scans = new Scan[writables.length];
    for (int i = 0; i < writables.length; i++) {
        scans[i] = (Scan) writables[i];
    }

    return scans;
}

From source file:com.cloudera.recordbreaker.analyzer.UnknownTextSchemaDescriptor.java

public UnknownTextSchemaDescriptor(DataDescriptor dd, String schemaRepr, byte[] miscPayload)
        throws IOException {
    super(dd, schemaRepr);
    this.randId = new Random().nextInt();

    // Deserialize the payload string into the parser
    DataInputStream in = new DataInputStream(new ByteArrayInputStream(miscPayload));
    try {/*from www  .  j a v  a2s .  c  o  m*/
        this.typeTree = InferredType.readType(in);
    } finally {
        in.close();
    }
}

From source file:HttpHEADMIDlet.java

private String requestUsingGET(String URLString) throws IOException {
    HttpConnection hpc = null;/*from   ww  w . ja va 2  s .  c o m*/
    DataInputStream dis = null;

    String content = "";
    try {
        hpc = (HttpConnection) Connector.open(URLString);
        hpc.setRequestMethod(HttpConnection.HEAD);

        dis = new DataInputStream(hpc.openInputStream());
        int i = 1;
        String key = "";
        String value = "";

        while ((value = hpc.getHeaderField(i)) != null) {
            key = hpc.getHeaderFieldKey(i++);
            content += key + ":" + value + "\n";
        }
        if (hpc != null)
            hpc.close();
        if (dis != null)
            dis.close();
    } catch (IOException e2) {
    }

    return content;
}

From source file:com.splunk.hunk.input.packet.DnsPcapRecordReader.java

@Override
public void vixInitialize(VixInputSplit split, TaskAttemptContext context)
        throws IOException, InterruptedException {
    //gLogger.info("takashi:TgzPacketRecordReader:vixInitialize is called");

    FileSystem fs = FileSystem.get(context.getConfiguration());
    fsDataIn = fs.open(split.getPath());
    pcapReader = initPcapReader(packet_type_class, new DataInputStream(fsDataIn));

    packetIterator = pcapReader.iterator();
    totalBytesToRead = split.getLength() - split.getStart();
    objectPreProcessor = new DnsPacketEventProcessor();
}

From source file:edu.umd.shrawanraina.BooleanRetrievalCompressed.java

private PairOfWritables<IntWritable, ArrayListWritable<PairOfInts>> readPost(BytesWritable value)
        throws IOException {
    byte[] val = value.getBytes();

    ByteArrayInputStream postings = new ByteArrayInputStream(val);
    DataInputStream stream = new DataInputStream(postings);

    ArrayListWritable<PairOfInts> pInt = new ArrayListWritable<PairOfInts>();
    int currentDocNo = 0;
    int dgap = 0;
    int tf = -1;/*from w w w .  j av  a 2 s. c  o  m*/
    int postingList = WritableUtils.readVInt(stream);

    for (int i = 0; i < postingList; i++) {
        dgap = WritableUtils.readVInt(stream);
        tf = WritableUtils.readVInt(stream);
        currentDocNo = currentDocNo + dgap;
        pInt.add(new PairOfInts(currentDocNo, tf));
    }
    //System.out.println("posting: "+new IntWritable(postingList));
    //System.out.println("pInt: "+pInt);
    return new PairOfWritables<IntWritable, ArrayListWritable<PairOfInts>>(new IntWritable(postingList), pInt);
}

From source file:com.splunk.hunk.input.packet.HttpPcapRecordReader.java

@Override
public void vixInitialize(VixInputSplit split, TaskAttemptContext context)
        throws IOException, InterruptedException {
    //gLogger.info("takashi:TgzPacketRecordReader:vixInitialize is called");

    FileSystem fs = FileSystem.get(context.getConfiguration());
    fsDataIn = fs.open(split.getPath());
    pcapReader = initPcapReader(packet_type_class, new DataInputStream(fsDataIn));

    packetIterator = pcapReader.iterator();
    totalBytesToRead = split.getLength() - split.getStart();
    objectPreProcessor = new HttpPacketEventProcessor();
}

From source file:com.netflix.aegisthus.input.readers.SSTableRecordReader.java

@SuppressWarnings("rawtypes")
@Override/* w w w  . j  av  a  2  s .co  m*/
public void initialize(InputSplit inputSplit, TaskAttemptContext ctx) throws IOException, InterruptedException {
    AegSplit split = (AegSplit) inputSplit;

    start = split.getStart();
    //TODO: This has a side effect of setting compressionmetadata. remove this.
    InputStream is = split.getInput(ctx.getConfiguration());
    if (split.isCompressed()) {
        end = split.getCompressionMetadata().getDataLength();
    } else {
        end = split.getEnd();
    }
    outputFile = ctx.getConfiguration().getBoolean("aegsithus.debug.file", false);
    filename = split.getPath().toUri().toString();

    LOG.info(String.format("File: %s", split.getPath().toUri().getPath()));
    LOG.info("Start: " + start);
    LOG.info("End: " + end);
    if (ctx instanceof TaskInputOutputContext) {
        context = (TaskInputOutputContext) ctx;
    }

    try {
        scanner = new SSTableScanner(new DataInputStream(is), split.getConvertors(), end,
                Descriptor.fromFilename(filename).version);
        if (ctx.getConfiguration().get("aegisthus.maxcolsize") != null) {
            scanner.setMaxColSize(ctx.getConfiguration().getLong("aegisthus.maxcolsize", -1L));
            LOG.info(String.format("aegisthus.maxcolsize - %d",
                    ctx.getConfiguration().getLong("aegisthus.maxcolsize", -1L)));
        }
        scanner.skipUnsafe(start);
        this.pos = start;
    } catch (IOException e) {
        throw new IOError(e);
    }
}

From source file:J2MEWriteReadMixedDataTypesExample.java

public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
        destroyApp(true);//www  .  ja va2s  . c  o  m
        notifyDestroyed();
    } else if (command == start) {
        try {
            recordstore = RecordStore.openRecordStore("myRecordStore", true);
            byte[] outputRecord;
            String outputString = "First Record";
            int outputInteger = 15;
            boolean outputBoolean = true;
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            DataOutputStream outputDataStream = new DataOutputStream(outputStream);
            outputDataStream.writeUTF(outputString);
            outputDataStream.writeBoolean(outputBoolean);
            outputDataStream.writeInt(outputInteger);
            outputDataStream.flush();
            outputRecord = outputStream.toByteArray();
            recordstore.addRecord(outputRecord, 0, outputRecord.length);
            outputStream.reset();
            outputStream.close();
            outputDataStream.close();
            String inputString = null;
            int inputInteger = 0;
            boolean inputBoolean = false;
            byte[] byteInputData = new byte[100];
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
            DataInputStream inputDataStream = new DataInputStream(inputStream);
            for (int x = 1; x <= recordstore.getNumRecords(); x++) {
                recordstore.getRecord(x, byteInputData, 0);
                inputString = inputDataStream.readUTF();
                inputBoolean = inputDataStream.readBoolean();
                inputInteger = inputDataStream.readInt();
                inputStream.reset();
            }
            inputStream.close();
            inputDataStream.close();
            alert = new Alert("Reading", inputString + " " + inputInteger + " " + inputBoolean, null,
                    AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
            recordstore.closeRecordStore();
            if (RecordStore.listRecordStores() != null) {
                RecordStore.deleteRecordStore("myRecordStore");
            }
        } catch (Exception error) {
            alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
    }
}