Example usage for java.io DataInputStream readUTF

List of usage examples for java.io DataInputStream readUTF

Introduction

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

Prototype

public final String readUTF() throws IOException 

Source Link

Document

See the general contract of the readUTF method of DataInput.

Usage

From source file:io.fluo.core.impl.Environment.java

private static Map<Column, ObserverConfiguration> readObservers(DataInputStream dis) throws IOException {

    HashMap<Column, ObserverConfiguration> omap = new HashMap<>();

    int num = WritableUtils.readVInt(dis);
    for (int i = 0; i < num; i++) {
        Column col = new Column();
        col.readFields(dis);/*from  w w w .  java 2s.  c om*/
        String clazz = dis.readUTF();
        Map<String, String> params = new HashMap<>();
        int numParams = WritableUtils.readVInt(dis);
        for (int j = 0; j < numParams; j++) {
            String k = dis.readUTF();
            String v = dis.readUTF();
            params.put(k, v);
        }

        ObserverConfiguration observerConfig = new ObserverConfiguration(clazz);
        observerConfig.setParameters(params);

        omap.put(col, observerConfig);
    }

    return omap;
}

From source file:uk.ac.ebi.mdk.io.ReactionMatrixIO.java

public static StoichiometricMatrix readCompressedBasicStoichiometricMatrix(InputStream stream,
        StoichiometricMatrix s) throws IOException {

    DataInputStream in = new DataInputStream(stream);

    int n = in.readInt();
    int m = in.readInt();

    s.ensure(n, m);//from  w  w w  .  ja  va 2 s.c  om

    for (int j = 0; j < m; j++) {
        s.setReaction(j, in.readUTF());
    }

    for (int i = 0; i < n; i++) {
        s.setMolecule(i, in.readUTF());
    }

    boolean convert = in.readBoolean();
    int size = in.readInt();

    while (--size >= 0) {

        int i = in.readInt();
        int j = in.readInt();
        Object value = convert ? in.readInt() : in.readDouble();
        Double dValue = value instanceof Double ? (Double) value : ((Integer) value).doubleValue();
        s.setValue(i, j, dValue);
    }

    in.close();

    return s;

}

From source file:org.wahtod.wififixer.utility.LogUtil.java

private static String getStackTrace(Context context) {
    StringBuilder trace = new StringBuilder();
    DataInputStream d;
    try {/*from   ww w.  j av a  2 s  . c  om*/
        d = new DataInputStream(context.openFileInput(DefaultExceptionHandler.EXCEPTIONS_FILENAME));
    } catch (FileNotFoundException e1) {
        return trace.append(context.getString(R.string.no_stack_trace_found)).toString();
    }
    for (;;) {
        try {
            trace.append(d.readUTF());
        } catch (EOFException e) {
            trace.append(e);
        } catch (IOException e) {
            trace.append(e);
        } finally {
            try {
                d.close();
            } catch (IOException e) {
                trace.append(e);
            }
        }
        context.deleteFile(DefaultExceptionHandler.EXCEPTIONS_FILENAME);
        return trace.toString();
    }
}

From source file:org.apache.jackrabbit.core.persistence.util.Serializer.java

/**
 * Deserializes a <code>PropertyState</code> object from the given binary
 * <code>stream</code>. Binary values are retrieved from the specified
 * <code>BLOBStore</code>./*from   w  w  w.ja  va2 s. com*/
 *
 * @param state     <code>state</code> to deserialize
 * @param stream    the stream where the <code>state</code> should be
 *                  deserialized from
 * @param blobStore handler for BLOB data
 * @throws Exception if an error occurs during the deserialization
 * @see #serialize(PropertyState, OutputStream, BLOBStore)
 */
public static void deserialize(PropertyState state, InputStream stream, BLOBStore blobStore) throws Exception {
    DataInputStream in = new DataInputStream(stream);

    // type
    int type = in.readInt();
    state.setType(type);
    // multiValued
    boolean multiValued = in.readBoolean();
    state.setMultiValued(multiValued);
    // definitionId
    in.readUTF();
    // modCount
    short modCount = in.readShort();
    state.setModCount(modCount);
    // values
    int count = in.readInt(); // count
    InternalValue[] values = new InternalValue[count];
    for (int i = 0; i < count; i++) {
        InternalValue val;
        if (type == PropertyType.BINARY) {
            String s = in.readUTF(); // value (i.e. blobId)
            // special handling required for binary value:
            // the value stores the id of the BLOB data
            // in the BLOB store
            if (blobStore instanceof ResourceBasedBLOBStore) {
                // optimization: if the BLOB store is resource-based
                // retrieve the resource directly rather than having
                // to read the BLOB from an input stream
                FileSystemResource fsRes = ((ResourceBasedBLOBStore) blobStore).getResource(s);
                val = InternalValue.create(fsRes);
            } else {
                InputStream is = blobStore.get(s);
                try {
                    val = InternalValue.create(is);
                } finally {
                    try {
                        is.close();
                    } catch (IOException e) {
                        // ignore
                    }
                }
            }
        } else {
            /**
             * because writeUTF(String) has a size limit of 65k,
             * Strings are serialized as <length><byte[]>
             */
            //s = in.readUTF();   // value
            int len = in.readInt(); // lenght of byte[]
            byte[] bytes = new byte[len];
            in.readFully(bytes); // byte[]
            String s = new String(bytes, ENCODING);
            val = InternalValue.valueOf(s, type);
        }
        values[i] = val;
    }
    state.setValues(values);
}

From source file:com.almalence.googsharing.Thumbnail.java

public static Thumbnail loadFrom(File file) {
    Uri uri = null;//from ww  w.  j a  v  a  2  s.  c om
    Bitmap bitmap = null;
    FileInputStream f = null;
    BufferedInputStream b = null;
    DataInputStream d = null;
    try {
        f = new FileInputStream(file);
        b = new BufferedInputStream(f, BUFSIZE);
        d = new DataInputStream(b);
        uri = Uri.parse(d.readUTF());
        bitmap = BitmapFactory.decodeStream(d);
        d.close();
    } catch (IOException e) {
        Log.i(TAG, "Fail to load bitmap. " + e);

        return null;
    } finally {
        Util.closeSilently(f);
        Util.closeSilently(b);
        Util.closeSilently(d);
    }

    Thumbnail thumbnail = createThumbnail(uri, bitmap, null, 0);
    if (thumbnail != null)
        thumbnail.setFromFile(true);

    return thumbnail;
}

From source file:com.alibaba.jstorm.yarn.utils.JstormYarnUtils.java

private static String readContent(String filePath) throws IOException {
    DataInputStream ds = null;
    try {/*from   w w w  .ja  v a 2s  . co  m*/
        ds = new DataInputStream(new FileInputStream(filePath));
        return ds.readUTF();
    } finally {
        org.apache.commons.io.IOUtils.closeQuietly(ds);
    }
}

From source file:com.android.hierarchyviewerlib.device.DeviceBridge.java

private static boolean readLayer(DataInputStream in, PsdFile psd) {
    try {//from   w  w  w  .ja  v  a2 s.c o m
        if (in.read() == 2) {
            return false;
        }
        String name = in.readUTF();
        boolean visible = in.read() == 1;
        int x = in.readInt();
        int y = in.readInt();
        int dataSize = in.readInt();

        byte[] data = new byte[dataSize];
        int read = 0;
        while (read < dataSize) {
            read += in.read(data, read, dataSize - read);
        }

        ByteArrayInputStream arrayIn = new ByteArrayInputStream(data);
        BufferedImage chunk = ImageIO.read(arrayIn);

        // Ensure the image is in the right format
        BufferedImage image = new BufferedImage(chunk.getWidth(), chunk.getHeight(),
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        g.drawImage(chunk, null, 0, 0);
        g.dispose();

        psd.addLayer(name, image, new Point(x, y), visible);

        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:com.google.gwt.dev.javac.CachedCompilationUnit.java

public static CachedCompilationUnit load(InputStream inputStream, JsProgram jsProgram) throws Exception {
    DataInputStream dis = new DataInputStream(new BufferedInputStream(inputStream));
    try {//from w w w.  j  a v  a  2  s  . c  o m
        CachedCompilationUnit compilationUnit = new CachedCompilationUnit();
        // version
        long version = dis.readLong();
        if (version != CompilationUnitDiskCache.CACHE_VERSION) {
            return null;
        }
        // some simple stuff :)
        compilationUnit.m_lastModified = dis.readLong();
        compilationUnit.m_displayLocation = dis.readUTF();
        compilationUnit.m_typeName = dis.readUTF();
        compilationUnit.m_contentId = new ContentId(dis.readUTF());
        compilationUnit.m_isSuperSource = dis.readBoolean();
        // compiled classes
        {
            int size = dis.readInt();
            compilationUnit.m_compiledClasses = new ArrayList<CompiledClass>(size);
            for (int i = 0; i < size; ++i) {
                // internal name
                String internalName = dis.readUTF();
                // is local
                boolean isLocal = dis.readBoolean();
                // bytes
                int byteSize = dis.readInt();
                byte[] bytes = new byte[byteSize];
                dis.readFully(bytes);
                // enclosing class
                CompiledClass enclosingClass = null;
                String enclosingClassName = dis.readUTF();
                if (!StringUtils.isEmpty(enclosingClassName)) {
                    for (CompiledClass cc : compilationUnit.m_compiledClasses) {
                        if (enclosingClassName.equals(cc.getInternalName())) {
                            enclosingClass = cc;
                            break;
                        }
                    }
                }
                // some assertion
                if (!StringUtils.isEmpty(enclosingClassName) && enclosingClass == null) {
                    throw new IllegalStateException("Can't find the enclosing class \"" + enclosingClassName
                            + "\" for \"" + internalName + "\"");
                }
                // init unit
                CompiledClass cc = new CompiledClass(internalName, bytes, isLocal, enclosingClass);
                cc.initUnit(compilationUnit);
                compilationUnit.m_compiledClasses.add(cc);
            }
        }
        // dependencies
        {
            compilationUnit.m_dependencies = new HashSet<ContentId>();
            int size = dis.readInt();
            if (size > 0) {
                for (int i = 0; i < size; i++) {
                    compilationUnit.m_dependencies.add(new ContentId(dis.readUTF()));
                }
            }
        }
        // JSNI methods
        {
            compilationUnit.m_jsniMethods = new ArrayList<JsniMethod>();
            int size = dis.readInt();
            if (size > 0) {
                for (int i = 0; i < size; i++) {
                    String name = dis.readUTF();
                    int startPos = dis.readInt();
                    int endPos = dis.readInt();
                    int startLine = dis.readInt();
                    String source = dis.readUTF();
                    String fileName = compilationUnit.m_displayLocation;
                    SourceInfo jsInfo = SourceOrigin.create(startPos, endPos, startLine, fileName);
                    compilationUnit.m_jsniMethods
                            .add(JsniCollector.restoreJsniMethod(name, source, jsInfo, jsProgram));
                }
            }
        }
        // Method lookup
        {
            compilationUnit.m_methodArgs = MethodArgNamesLookup.load(dis);
        }
        return compilationUnit;
    } finally {
        IOUtils.closeQuietly(dis);
    }
}

From source file:com.momock.http.HttpSession.java

public static DownloadInfo getDownloadInfo(File file) {
    if (file.exists()) {
        return new DownloadInfo(file.length(), file.length());
    }/* www.  ja  v a  2  s .c o m*/
    DownloadInfo di = null;
    File fileData = new File(file.getPath() + ".data");
    File fileInfo = new File(file.getPath() + ".info");
    if (fileData.exists() && fileInfo.exists()) {
        long downloadedLength = fileData.length();
        long contentLength = -1;
        DataInputStream din;
        if (fileInfo.length() == 0)
            return new DownloadInfo(0, 0);
        try {
            din = new DataInputStream(new FileInputStream(fileInfo));

            int headerCount = din.readInt();
            for (int i = 0; i < headerCount; i++) {
                String key = din.readUTF();
                int count = din.readInt();
                List<String> vals = new ArrayList<String>();
                for (int j = 0; j < count; j++) {
                    String val = din.readUTF();
                    vals.add(val);
                }
                if ("Content-Length".equals(key)) {
                    if (contentLength == -1)
                        contentLength = Convert.toInteger(vals.get(0));
                } else if ("Content-Range".equals(key)) {
                    int pos = vals.get(0).indexOf('/');
                    contentLength = Convert.toInteger(vals.get(0).substring(pos + 1));
                }
            }
            din.close();
            di = new DownloadInfo(downloadedLength, contentLength);
        } catch (Exception e) {
            Logger.error(e);
        }
    }
    return di;
}

From source file:sdn_competition.PcapReader_Main.java

static void readBinary(String file1) {
    try {//  w w w .j a va 2  s .c  om
        StringBuilder sb = new StringBuilder();
        File file = new File(file1);
        DataInputStream input = new DataInputStream(new FileInputStream(file));
        try {
            //     List<String> lines = IOUtils.readLines(input);
            while (true) {
                String temp = input.readUTF();

                System.out.println(temp);
                sb.append(temp);
            }
        } catch (EOFException eof) {
        } catch (IOException e) {
            e.printStackTrace();
        }
        //   System.out.println(sb.toString());
        writeToFile("Memory_Dump.txt", sb);
    } catch (FileNotFoundException e2) {
        e2.printStackTrace();
    }
}